[英]How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime?
How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime? CLR如何在运行时区分不同程序集中具有相同名称但没有名称空间的类?
Example : 范例:
Assembly : Assembly1 组装:组装1
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 1 class Foo");
}
}
Please note class Foo is not having any namespace. 请注意,类Foo没有任何名称空间。
Assembly : Assembly2 组装:Assembly2
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 2 class Foo");
}
}
Please note class Foo is not having any namespace here too. 请注意,Foo类在这里也没有任何名称空间。
So Assembly1 and Assembly2 are having classes with the same name but without namespace. 因此Assembly1和Assembly2的类具有相同的名称,但没有名称空间。
Assembly : TestAssembly1 ---->Refers : Assembly1 程序集:TestAssembly1 ---->引用:程序集1
namespace TestAssembly1
{
public class TestAssembly1Class
{
public void CallFooMethod()
{
new Foo().Test(); //How CLR would know this Foo is from Assembly 1 at runtime?
}
}
}
Assembly : TestAssembly2---------->Refers : Assembly2 程序集:TestAssembly2 ---------->引用:程序集2
namespace TestAssembly2
{
public class TestAssembly2Class
{
public void CallFooMethod()
{
new Foo().Test(); //How CLR would know this Foo is from Assembly 2 at runtime?
}
}
}
Now suppose There is one Executable say MyShell.exe which has Main method. 现在,假设有一个具有Main方法的可执行文件MyShell.exe。 which looks like following. 如下所示。
class Program
{
static void Main(string[] args)
{
new TestAssembly1Class().CallFooMethod();
new TestAssembly2Class().CallFooMethod();
}
}
So here question is that, both Foo classes are not having any namespace then, when MyShell.exe is executed how CLR will differentiate these conflicting Foo classes? 因此,这里的问题是,两个Foo类都不具有任何名称空间,那么在执行MyShell.exe时CLR如何区分这些冲突的Foo类?
How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime? CLR如何在运行时区分不同程序集中具有相同名称但没有名称空间的类?
Both types would have a different AssemblyQualifiedName
两种类型将具有不同的AssemblyQualifiedName
Because when the two assemblies are compiled independently prior to inclusion and the two types are each assigned a fully qualified name which includes the assembly name. 因为当两个程序集在包含之前进行独立编译时,并且为这两种类型分别分配了包含程序集名称的完全限定名称。
When the third assembly, which references the two previous assemblies, is compiled and names are bound the conflict is resolved via the fully qualified name. 当引用了前两个程序集的第三个程序集被编译并绑定了名称时,将通过完全限定的名称解决冲突 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.