How to statically declare a type at compile time by AssemblyQualifiedName in C# [duplicate]

StackOverflow https://stackoverflow.com/questions/13753731

  •  05-12-2021
  •  | 
  •  

Question

Possible Duplicate:
When must we use extern alias keyword in C#?

I have an assembly which is composed mostly of files shared from other projects in the solution. In other words, the whole solution would be about 10 MB, but I export a small subset of that functionality into a separate 200 KB assembly. All the projects in the solution are in the NUnit test project except the UI.

The problem is, I went to add a test for one of the shared files, and I got the following error:

The type 'xxx' exists in both 'c:\path\to\project\Logic.dll'
and 'c:\path\to\project\Exported.dll'.

I know I can use reflection and provide an assembly qualified name, or I could remove the exported assembly tests into a separate project, but both would be a pain. I simply want to specify to use the version of xxx in the Logic.dll at compile time. Is it possible to specify which version of the Type to use at compile time, so it looks like this:

xxx blah = new xxx();
blah.DoSomething(123);

instead of:

Type t = Type.GetType("Logic.xxx, Logic");
object blah = Activator.CreateInstance(t);
t.GetMethod("DoSomething").Invoke(blah, new object[] { 123 });
Was it helpful?

Solution 2

Hans Passant's comment is the correct answer: "extern alias" is what I was looking for, and the answers in When must we use extern alias keyword in C#? give good pointers to how to use it.

OTHER TIPS

You should be able to specify the reference based on the namespace the object class you want is located. Something along the lines of this:

LogicNamespace.XXX blah = new LogicNamespace.XXX();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top