【发布时间】:2014-01-01 13:41:59
【问题描述】:
CLR在运行时如何区分不同程序集中同名但没有命名空间的类?
例子:
组装:组装1
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 1 class Foo");
}
}
请注意类 Foo 没有任何命名空间。
组装:组装2
public class Foo
{
public void Test()
{
Console.WriteLine("Assembly 2 class Foo");
}
}
请注意类 Foo 在这里也没有任何命名空间。
因此,Assembly1 和 Assembly2 具有同名但没有命名空间的类。
Assembly:TestAssembly1 ---->参考:Assembly1
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---------->参考:Assembly2
namespace TestAssembly2
{
public class TestAssembly2Class
{
public void CallFooMethod()
{
new Foo().Test(); //How CLR would know this Foo is from Assembly 2 at runtime?
}
}
}
现在假设有一个可执行文件说 MyShell.exe 具有 Main 方法。如下所示。
class Program
{
static void Main(string[] args)
{
new TestAssembly1Class().CallFooMethod();
new TestAssembly2Class().CallFooMethod();
}
}
所以这里的问题是,两个 Foo 类都没有任何命名空间,那么当执行 MyShell.exe 时,CLR 将如何区分这些冲突的 Foo 类?
【问题讨论】:
-
就像您可以区分两个
new object()一样——虽然类型名称相同,但它们是 CLR 内存空间中的不同对象。
标签: c# .net namespaces