Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

c# - Use types of same name & namespace in 2 .NET assemblies

Out of curiosity, I've created 2 assemblies which both have a class (Class1) with the exact same namespace (Library1). I then create another client referencing those 2 assemblies and try to create an instance of Class1.

The compiler, not surprisingly, gives me a compile-error about the ambiguous reference. Is there any way to explicitly specify the type in the assembly I want to use to avoid the ambiguity?

Note: I know this rarely, if ever at all, happens in practice. It's just a question out of curiosity about language feature.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you should use an extern alias to wrap the assembly namespaces outside of the Global namespace. Here's how:

  1. In the project that references the 2 assemblies, select one of them under References, and in the Properties window change the Aliases value from global to, say, global, Library1a.

  2. Do the same for the the other reference, but give it a different alias-- let's go with global, Library1b for our example.

  3. Insert extern alias Library1a; and/or extern alias Library1b; as the first 2 lines of any classes that consume the assemblies.

  4. When accessing ambiguous members, qualify the namespace with Library1a. or Library1b.. Examples: Library1a.Library1.Class1... Library1b.Library1.Class1...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...