开发者

use 1 class which exists in 2 references

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?

开发者_如何学CA 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.


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));
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜