Inner classes with the same namespace, but in different assemblies?

Basically I have a CS file defining an inner class, then I copy the various CS projects. And the builds of these CS projects will be uploaded to the application. The program is working fine.

However, having multiple classes with the same class name in the same namespace makes me feel awkward, even if they are internal in each assembly.

Is the class uniquely identified through the class name, namespace, and assembly in AppDomain?

+5
source share
3 answers

Is the class uniquely identified through the class name, namespace, and assembly in AppDomain?

The short answer is yes.

Longer answer:

You can consider a few subtle points.

First, from the perspective of the CLR, there is no such thing as a namespace. System.String string type name in relation to the CLR. Therefore, it is more accurate to say that from the point of view of the CLR, a type is uniquely identified by name and assembly.

Secondly, types can be nested. Thus, types are actually uniquely identified by their name containing the type (if there is one) and the assembly.

Third, types can be shared. Foo.Bar and Foo.Bar<T> are different types. Thus, types are identified by their name containing the type, assembly, and general arity.

Fourthly, and this is strange, the CLR considers types in assemblies loaded with Load different from types in the same assembly loaded with LoadFrom . You may encounter situations where the CLR tells you that the Foo type in the Bar assembly is incompatible with the Foo type in the assembly panel, and the boy is confused.

+8
source

While everything confuses all your inner classes, it exists only in their respective assemblies, therefore they are autonomous and will not be visible to other classes in another assembly.

But in terms of service and simplicity, this should be put in first place in the house where the tasks are performed.

+1
source

Yes, the CLR uses an assembly to identify each type. If you compile a simple Hello World application and look at IL:

 .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "Hello world" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: nop IL_000c: ret } // end of method Program::Main 

You can see how System.Console is referenced, including assembly names, namespaces, and class names. However, if you refer to a type in the same assembly, you can specify the name of the assembly, for example:

 .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "Hello world" IL_0006: call void ConsoleApplication1.Foo::Write(string) IL_000b: nop IL_000c: ret } // end of method Program::Main 
+1
source

Source: https://habr.com/ru/post/1240587/


All Articles