出于好奇,我创建了 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模板网!
有没有办法知道是否有人为您的网站添加了书签Is there a way to know if someone has bookmarked your website?(有没有办法知道是否有人为您的网站添加了书签?)
使用不同的 .Net 语言?Use of Different .Net Languages?(使用不同的 .Net 语言?)
是否有执行 Excel NORMINV 函数的 C# 库?Is there a C# library that will perform the Excel NORMINV function?(是否有执行 Excel NORMINV 函数的 C# 库?)
确定 ASP.NET 站点的“活动"用户数Determining an #39;active#39; user count of an ASP.NET site(确定 ASP.NET 站点的“活动用户数)
从 C# 中的加权列表中选择 x 个随机元素(无需替换Select x random elements from a weighted list in C# (without replacement)(从 C# 中的加权列表中选择 x 个随机元素(无需替换))
跟踪当前在线用户的最佳方式Best way to keep track of current online users(跟踪当前在线用户的最佳方式)