简体   繁体   English

当两个引用的程序集都定义类型A.A1时遇到问题

[英]Having problems when two of referenced assemblies both define type A.A1

If two assemblies both define namespace A containing class A1 , then the two classes are considered unique types. 如果两个程序集都定义包含class A1 namespace A ,则这两个类被视为唯一类型。

a) Are the two namespaces also considered unique? a)两个名称空间是否也被认为是唯一的?

b) If program P has a reference to both assemblies, how do we create an instances of the two types? b)如果program P有两个程序集的引用,我们如何创建这两种类型的实例? Namely, I keep getting an error when I try to create an instance of A.A1 也就是说,当我尝试创建A.A1的实例时,我一直收到错误

using A;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A1 a = new A1(); // error
        }
  }
}

c) But if program P also defines type B.A1 , then compiler doesn't complain when I declare an instance of A1 : c)但是如果program P也定义了type B.A1 ,那么当我声明A1的实例时,编译器不会抱怨:

using A;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A1 a = new A1(); // ok
        }
    }

    class A1 { }
}

Shouldn't compiler complain, since it can't know which version of A1 to use ( A.A1 from one of the referenced assemblies or B.A1 )? 不应该编译器抱怨,因为它无法知道使用哪个版本的A1A.A1来自其中一个引用的程序集或B.A1 )?

thanx 感谢名单

You can resolve this with the extern alias directive. 您可以使用extern alias指令解决此问题。

And here is a better explanation . 这是一个更好的解释

Referencing two assemblies having the same namespaces and same members within those namespaces is a complete no-no (ie don't do it!). 在这些命名空间中引用具有相同名称空间和相同成员的两个程序集是完全禁止的(即不要这样做!)。 I you have control over one or other of the assemblies, ensure that the root namespaces are different for the two and then you can disambiguate references to members within the assembly/namespace hierarchy. 我可以控制一个或另一个程序集,确保两个根名称空间不同,然后您可以消除对程序集/名称空间层次结构中成员的引用的歧义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在两个引用的程序集之间键入歧义 - Type ambiguity between two referenced assemblies 程序集引用问题-'程序集未引用'与两个程序集的类型 - assembly reference problem--'assembly not referenced' versus type in two assemblies 当引用的程序集引用mscorlib 2.0.5.0和4.0.0.0时,如何让roslyn编译 - How to get roslyn to compile when referenced assemblies have references to both mscorlib 2.0.5.0 and 4.0.0.0 在两个引用的程序集中出现的引用特定名称空间 - Reference Particular Namespace that appears in two referenced Assemblies Castle DynamicProxy Interceptor具有不同程序集的问题 - Castle DynamicProxy Interceptor having problems with different assemblies 控制何时加载引用的.NET程序集 - Control when referenced .NET assemblies are loaded 对具有引用程序集的应用程序进行数字签名的正确方法 - Proper way to digitally sign the application having referenced assemblies 两个程序集中定义了相同的类型 - The same type is defined in two assemblies 即使我限定了该类型,当该类型存在于两个程序集中时,为什么会出现错误? - Why do I get an error when this type exists in two assemblies even though I qualified the type? MVC3类型存在于两个不同的程序集中 - MVC3 Type exists in two different assemblies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM