[英]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
)? 不应该编译器抱怨,因为它无法知道使用哪个版本的
A1
( A.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.