问题描述
出于好奇,我创建了 2 个程序集,它们都有一个具有完全相同命名空间 (Library1) 的类 (Class1).然后我创建另一个引用这两个程序集的客户端并尝试创建 Class1 的实例.
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.
推荐答案
我认为你应该使用 extern alias 将程序集命名空间包装在全局命名空间之外.方法如下:
I think you should use an extern alias to wrap the assembly namespaces outside of the Global namespace. Here's how:
在引用 2 个程序集的项目中,在引用"下选择其中一个,然后在属性"窗口中将别名值从
global更改为,例如,global, Library1a.
对另一个引用做同样的事情,但给它一个不同的别名——让我们以 global, Library1b 为例.
Do the same for the the other reference, but give it a different alias-- let's go with global, Library1b for our example.
插入 extern alias Library1a; 和/或 extern alias Library1b; 作为使用程序集的任何类的前 2 行.
Insert extern alias Library1a; and/or extern alias Library1b; as the first 2 lines of any classes that consume the assemblies.
访问不明确的成员时,使用 Library1a. 或 Library1b. 限定命名空间.示例:Library1a.Library1.Class1... Library1b.Library1.Class1...
When accessing ambiguous members, qualify the namespace with Library1a. or Library1b.. Examples: Library1a.Library1.Class1... Library1b.Library1.Class1...
这篇关于使用同名类型 &2 个 .NET 程序集中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)