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

梦想的初衷 提交于 2019-12-06 07:21:40

问题


If two assemblies both define namespace A containing class A1, then the two classes are considered unique types.

a) Are the two namespaces also considered unique?

b) If program P has a reference to both assemblies, how do we create an instances of the two types? Namely, I keep getting an error when I try to create an instance of 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:

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 )?

thanx


回答1:


You can resolve this with the extern alias directive.

And here is a better explanation.




回答2:


Referencing two assemblies having the same namespaces and same members within those namespaces is a complete no-no (i.e. 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.



来源:https://stackoverflow.com/questions/3272907/having-problems-when-two-of-referenced-assemblies-both-define-type-a-a1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!