2

I have third party .Net assembly lets say ThridParty.dll which uses type "dotnet-namespace.dotnet-type from .Net assembly dotnetassembly.dll. Now in new version of dotnetassembly.dll "dotnet-type" which was earlier in dotnet-namespace has been moved to new namespace lets say new-dotnet-namespace. Fully qualified name of dotnet-type has become new-dotnet-namespace.dotnet-type.

Now my question is, Is there any way I can tell runtime to look for type dotnet-type in new namespace i.e. new-dotnet-namespace instead of old namespace i.e. dotnet-namespace?

I know we can do use assembly redirection when we want to point new version of assembly but is it possible to redirect types within same assembly but to different namespace? I don't have option to get new version of thridparty.dll which uses type from new namespace.

3
  • 1
    Accomplishing this would be a very large undertaking. You would have to write the code that can do some sort of decompilation and parse through the result to find what you are looking for. Commented Jul 1, 2016 at 21:25
  • 1
    Well, of course, recompile your code. Assuming that anything else is possible and practical is nonsensical and unproductive. Microsoft put a pretty solid foot down on this kind of waffling. If you want to use Reflection then nobody here is going to stop you. Commented Jul 1, 2016 at 21:25
  • Agreed with other comments. You could, but I'd rather have words with the people who chose to change the namespace. Commented Jul 2, 2016 at 4:19

2 Answers 2

3

No, there is nothing in .Net Framework to redirect one type to another.

Note that namespace is just syntactic sugar to makes names shorter in source code, for .Net itself namespace is just part of the type name - so your question is essentially "can I point on type to another".

Solutions:

  • recompile
  • build proxy assembly
  • rewrite IL to point to new types.
Sign up to request clarification or add additional context in comments.

1 Comment

LOL ... that's too much effort than probably changing the code and modify the namespace.
0

How about:

public Type FindType(string typeName, string assemblyName)
{
    Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(a => a.GetName().Name == assemblyName);
    return assembly != null ? assembly.GetTypes().SingleOrDefault(t => t.Name == typeName) : null;
}

You would call this:

Type dotNetType = FindType("dotnet-type", "dotnetassembly");

This way you are independent of the type namespace.

As a side note, if you have two types with the same name in that assembly, this method will simply not work because it won't be able to know which type to return (in fact, it'll throw an exception). You can change SingleOrDefault to FirstOrDefault and make it return the first occurence.

Of course you'd lose all the compiler-time advantages, since you'll be looking up the type at runtime.

2 Comments

What if there are multiple types with the same typeName in the assembly?
That's a good question. I should clarify this won't work for types with the same name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.