[英]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
). 出于好奇,我创建了两个程序集,它们都有一个类(
Class1
),具有完全相同的命名空间( Library1
)。 I then create another client referencing those 2 assemblies and try to create an instance of Class1
. 然后,我创建另一个引用这两个程序集的客户端,并尝试创建
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.
这只是一个关于语言特征的好奇心的问题。
I think you should use an extern alias to wrap the assembly namespaces outside of the Global namespace. 我认为您应该使用extern别名将程序集名称空间包装在Global名称空间之外。 Here's how:
这是如何做:
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个程序集的项目中,在“引用”下选择其中一个,并在“属性”窗口中将“
global
别名”值更改为“ global, Library1a
。
Do the same for the the other reference, but give it a different alias-- let's go with global, Library1b
for our example. 对另一个参考做同样的事情,但给它一个不同的别名 - 让我们用
global, Library1b
作为我们的例子。
Insert extern alias Library1a;
插入
extern alias Library1a;
and/or extern alias Library1b;
和/或
extern alias Library1b;
as the first 2 lines of any classes that consume the assemblies. 作为使用程序集的任何类的前2行。
When accessing ambiguous members, qualify the namespace with Library1a.
访问不明确的成员时,使用
Library1a.
限定命名空间Library1a.
or Library1b.
或
Library1b.
. 。 Examples:
Library1a.Library1.Class1...
Library1b.Library1.Class1...
示例:
Library1a.Library1.Class1...
Library1b.Library1.Class1...
It happens in practice and is a real pain, the external alias can't always solve the problem. 它在实践中发生并且是一种真正的痛苦,外部别名并不能总是解决问题。 Here's one example of where it's an issue Duplicate Namepsaces .
这是一个问题,它是一个问题重复名称空间的一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.