简体   繁体   English

CLR如何在运行时区分不同程序集中具有相同名称但没有名称空间的类?

[英]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.

相关问题 在两个不同的程序集中,两个具有相同名称的类可以位于相同的名称空间中吗? - Can two classes with the same name be in the same namespace across two different assemblies? 用相同的名称/命名空间实例化不同的类 - Instantiate different classes with the same name/namespace 名称 &lt;...&gt; 不存在于命名空间 clr-namespace &lt;...&gt; - the name <...> does not exist in the namespace clr-namespace <...> 在2个.NET程序集中使用相同名称和命名空间的类型 - Use types of same name & namespace in 2 .NET assemblies .net引用的程序集名称如何在运行时由clr解析? - How is the .net referenced assemblies names resolved by the clr at runtime? C#WPF-名称“…”在命名空间“ clr-namespace:…”中不存在 - C# WPF - The name “…” does not exist in the namespace “clr-namespace:…” 名称“XYZ”在名称空间“clr-namespace:ABC”中不存在 - The name “XYZ” does not exist in the namespace “clr-namespace:ABC” 内部类具有相同的命名空间但在不同的程序集中? - Internal classes having the same namespaces but in different assemblies? 名称“ X”在名称空间“ clr-namespace:Y”中不存在 - The name “X” does not exist in the namespace “clr-namespace:Y” 如何区分具有相同名称的不同后代? - How do I differentiate between different descendents with the same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM