Namespace collisions

前端 未结 3 736
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 12:57

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

相关标签:
3条回答
  • 2020-12-19 13:15

    Is your code in namespace A.B or A.B.C? If so, that's probably the issue. Use a using directive like this:

    using TheTypeIWant = A.B.C.D.MyType;
    

    then just refer to TheTypeIWant in your code.

    EDIT: I've just tried the "using MyType=A.B.C.D.MyType" option, but that doesn't work. The above is fine though.

    0 讨论(0)
  • 2020-12-19 13:20

    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:

    ... for each namespace N, starting with the namespace in which the namespace-or-typename occurs, continuing with each enclosing namespace (if any), and ending with the global namespace, the following steps are evaluated until an entity is located...

    and then followed by:

    If K is zero and the namespace declaration contains an extern-alias-directive or using-aliasdirective that associates the name I with an imported namespace or type, then the namespace-or-type-name refers to that namespace or type

    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 using clause are searched.

    The following example prints "Ns1.Foo"

    using Ns1.Foo.Foo2;
    
    namespace Ns1.Foo
    {
        class Foo
        {
            public void Print()
            {
                System.Console.WriteLine("Ns1.Foo");
            }
        }
    }
    
    namespace Ns1.Foo.Foo2
    {
        class Foo
        {
            public void Print()
            {
                System.Console.WriteLine("Ns1.Foo.Foo2");
            }
        }
    }
    
    namespace Ns1.Foo.Bar
    {
        class Bar
        {
            public void Print()
            {
                new Foo().Print();
            }
    
            static void Main()
            {
                new Bar().Print();
            }
        }
    }
    

    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:

    namespace Ns1.Foo.Bar
    {
        using Ns1.Foo.Foo2;
        class Bar
        {
            public void Print()
            {
                new Foo().Print();
            }
    
            static void Main()
            {
                new Bar().Print();
            }
        }
    }
    

    and Ns1.Foo.Foo2 will be printed.

    Edit: changed example

    0 讨论(0)
  • 2020-12-19 13:24

    Just a guess: in your project properties, is the "default namespace" set to A.B ?

    0 讨论(0)
提交回复
热议问题