简体   繁体   English

在2个.NET程序集中使用相同名称和命名空间的类型

[英]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: 这是如何做:

  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个程序集的项目中,在“引用”下选择其中一个,并在“属性”窗口中将“ global别名”值更改为“ 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. 对另一个参考做同样的事情,但给它一个不同的别名 - 让我们用global, Library1b作为我们的例子。

  3. 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行。

  4. 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.

相关问题 我一直遇到奇怪的问题,想知道是否是因为我使用与名称空间同名的类型? - Odd problem I keep having, wondering if its because I use types with same name as namespace? 在两个不同的程序集中,两个具有相同名称的类可以位于相同的名称空间中吗? - Can two classes with the same name be in the same namespace across two different assemblies? .NET 中程序集的标记类型是什么? - What are the marker types for assemblies in .NET? CLR如何在运行时区分不同程序集中具有相同名称但没有名称空间的类? - How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime? “名称类型与名称空间的名称相同”是否适用于名称空间模块? - Does the “Don't name types the same as namespaces” apply to namespace modules? 在多个程序集中使用相同的名称空间是一个好习惯吗? - Same namespace in multiple assemblies is a good practice? 如何在两种相同名称和内部结构之间进行转换,但是来自不同的程序集? - How to cast between 2 types of the same name and internal sturcture but from different assemblies? .NET程序集的强名称签名 - Strong name signing of .NET assemblies 如何从2个不同的程序集加载相同的插件,并在两者之间序列化和反序列化类型? C#.net - How to load same plugins from 2 different assemblies, and serializing and deserializing types between the two? C# .net 程序集和命名空间 - Assemblies and Namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM