Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal classes having the same namespaces but in different assemblies?

Tags:

c#

Basically I have a CS file defining an internal class, then I copy to various of CS projects. And the assemblies of these CS projects will be loaded in an application. The program seems to be running just fine.

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

Is a class uniquely identified through class name, namespace as well as assembly in an AppDomain?

like image 222
ZZZ Avatar asked Jan 14 '16 01:01

ZZZ


2 Answers

Is a class uniquely identified through class name, namespace as well as assembly in an AppDomain?

Short answer: yes.

Longer answer:

There are a few subtle points to consider.

First, from the CLR perspective there is no such thing as a "namespace". The name of the string type is System.String as far as the CLR is concerned. So it is more accurate to say that from the CLR perspective, a type is uniquely identified by its name and assembly.

Second, types may be nested. So types are actually uniquely identified by their name, containing type (if there is one) and assembly.

Third, types may be generic. Foo.Bar and Foo.Bar<T> are different types. So types are identified by their name, containing type, assembly, and generic arity.

Fourth, and this is the weird one, the CLR considers types in assemblies loaded with Load different than types in the same assembly loaded with LoadFrom. You can end up with situations where the CLR tells you that type Foo in assembly Bar is not compatible with type Foo in assembly Bar, and boy, is that confusing.

like image 78
Eric Lippert Avatar answered Dec 01 '22 01:12

Eric Lippert


While it makes things confusing all your internal classes only exist in their respective assemblies so they are autonomous and will not be seen by the other classes in the other assembly.

But from a maintenance and simplicity perspective it should be put at the top of the house keeping tasks.

like image 37
T McKeown Avatar answered Dec 01 '22 00:12

T McKeown