How is it possible that .NET is finding the wrong MyType in this scenario?
I have a type A.B.C.D.MyType in a project that I’m working on, and I’m referencing a DLL that has a type A.B.MyType. I do not have any using A.B; statements anywhere in my code, and I do have using A.B.C.D;. When I compile, the compiler thinks any naked reference to MyType means A.B.MyType.
I know I could just rename the class or use an alias, but I’m wondering how this is even possible. Any ideas?
Are you working in a namespace that is under A.B namespace? (for example A.B.X) if so the C# namespace resolutions (ECMA-334 C# Language Specification : 10.8 10.8 Namespace and type names) says:
and then followed by:
This means that name resolution starts at the current namespace and searches all namespaces up to the root, and only after this hierarchical search ends, then the namespaces imported with the
usingclause are searched.The following example prints ‘Ns1.Foo’
Edit: Adding a using clause inside a namespace, will make so that the namespace is searched before the hierarchical search of current namespace is done is done. Change the example to:
and
Ns1.Foo.Foo2will be printed.Edit: changed example