使用同名类型 &2 个 .NET 程序集中的命名空间 [英] Use types of same name & namespace in 2 .NET assemblies

查看:11
本文介绍了使用同名类型 &2 个 .NET 程序集中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇,我创建了 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:

  1. 在引用 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 程序集中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆