tags:

views:

49

answers:

2

In .NEt C# How can I tell the compiler to use a Type from a particular class when a second Type exists with the same name in my project references?

A class I wish to use exists in 2 project references. The namespace etc is identical. I require both references in the project, but for this particular class I wish to use the one from one of the references assemblies.

A: 

You may need to write a wrapper class in two seperate assemblies in which you expose the namespaces differently if you do not have access to change the namespace.

xkingpin
+4  A: 

You use extern aliases. Anson Horton has a walkthrough here.

It's a solution to a problem which you should avoid if at all possible, of course - but it does work.

Ironically enough, I've just been editing the section about extern aliases for the second edition of C# in Depth. Here's the same code, where both First.dll and Second.dll expose a type called "Demo.Example".

// Compile with
// csc Test.cs /r:FirstAlias=First.dll /r:SecondAlias=Second.dll

extern alias FirstAlias;
extern alias SecondAlias;

using System;
using FD = FirstAlias::Demo;
class Test
{
   static void Main()
   {
      Console.WriteLine(typeof(FD.Example)); 
      Console.WriteLine(typeof(SecondAlias::Demo.Example));
   }
}
Jon Skeet
@Jon: You're faster than your shadow! I was about to write something such as what you have linked. Hehehe... =)
Will Marcouiller
I'll give it a blast, I came across similar code somewhere, the additional compile args were putting me off.The assemblies are Microsoft's so I cannot change.Cheers.
learnerplates