简体   繁体   English

子命名空间的命名空间访问问题

[英]Namespace access issue with sub namespaces

I have two classes in the different sub namespaces: 我在不同的子命名空间中有两个类:

namespace Acme.ByteTools
{
        class ByteTools
        {
        ...
        }
}

namespace Acme.IO
{
        class Reader
        {
        ...
        }
}

While I trying to access Acme.ByteTools from the any third namespace, I use: 当我尝试从任何第三个命名空间访问Acme.ByteTools时,我使用:

using Acme.ByteTools;
...
ByteTools.BytesToUint(...);

but when I try to access Acme.ByteTools from Acme.IO, compiler require different notation: 但是当我尝试从Acme.IO访问Acme.ByteTools时,编译器需要不同的表示法:

using Acme.ByteTools;
...
ByteTools.ByteTools.BytesToUint(...);

Why? 为什么?

As others including the legendary Eric Lippert have stated...please don't create collisions. 包括传奇人物Eric Lippert在内的其他人也表示......请不要制造碰撞。 I've seen code riddled with using alias directives because of collisions and I simply cannot express how frustrating it is to see a namespace change its name from class to class. 我看到由于冲突而使用别名指令的代码很多,我根本无法表达看到命名空间从一个类更改为类的名称是多么令人沮丧。

The confusion speaks for itself. 混乱不言自明。 Just look at something like this: 看看这样的事情:

namespace ConsoleApplication1
{    
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();//A's a namespace
            A.A b = new A.A();//A is a namespace this works!
            global::A.A nuts = new A();//This fails...ugh
            Console.ReadLine();
        }
    }
}

namespace A
{    
    class A
    {
        public void DoWork()
        {
            A a = new A();//A's a class
            A.A b = new A.A();//A is a type (class) A.A makes no sense to the compiler
            global::A.A nuts = new A();//Oh but this works fine
        }

    }
}

So the fix is make sure the namespaces and classes are different. 因此,修复是确保命名空间和类不同。 A using alias directive using B = A; 使用别名指令using B = A; might ease the pain however that directive can change from file to file and anything to its right must be fully qualified. 可能会减轻痛苦,但是该指令可以在不同文件之间进行更改,并且其中的任何内容都必须完全合格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM