[英]Can two classes with the same name be in the same namespace across two different assemblies?
[英]How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime?
CLR如何在運行時區分不同程序集中具有相同名稱但沒有名稱空間的類?
范例:
組裝:組裝1
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 1 class Foo");
}
}
請注意,類Foo沒有任何名稱空間。
組裝:Assembly2
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 2 class Foo");
}
}
請注意,Foo類在這里也沒有任何名稱空間。
因此Assembly1和Assembly2的類具有相同的名稱,但沒有名稱空間。
程序集: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?
}
}
}
程序集: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?
}
}
}
現在,假設有一個具有Main方法的可執行文件MyShell.exe。 如下所示。
class Program
{
static void Main(string[] args)
{
new TestAssembly1Class().CallFooMethod();
new TestAssembly2Class().CallFooMethod();
}
}
因此,這里的問題是,兩個Foo類都不具有任何名稱空間,那么在執行MyShell.exe時CLR如何區分這些沖突的Foo類?
CLR如何在運行時區分不同程序集中具有相同名稱但沒有名稱空間的類?
兩種類型將具有不同的AssemblyQualifiedName
因為當兩個程序集在包含之前進行獨立編譯時,並且為這兩種類型分別分配了包含程序集名稱的完全限定名稱。
當引用了前兩個程序集的第三個程序集被編譯並綁定了名稱時,將通過完全限定的名稱解決沖突 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.