命名空间冲突 [英] Namespace collisions

查看:113
本文介绍了命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能,.NET是找错了的MyType在这种情况下?

How is it possible that .NET is finding the wrong 'MyType' in this scenario?

我曾经在一个项目型ABCDMyType是我的工作,而且我引用一个DLL,它有一个类型ABMyType?我没有任何'使用A·B;'声明我code的任何地方,而且我也有'用ABCD;。当我编译,编译器认为任何明火提及MyType的'手段'ABMyType。

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.

任何想法?

谢谢!

推荐答案

您的是AB下一个命名空间的工作? (例如:ABX)如果是C#的命名空间分辨率( ECMA-334 C#语言规范:10.8 10.8命名空间和类型名称)说:

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:

...对于每个命名空间N,开始   与命名空间中的   命名空间或类型名称时,   每个包容继续   命名空间(如果有的话),并与结束   全局命名空间,如下   步骤进行计算,直到实体   位于...

... 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...

,然后依次为:

如果K为零,并且命名空间   声明包含一个   的extern别名指示或   使用-aliasdirective的同伙   这个名字我与一个导入的命名空间   或类型,则   命名空间或类型名称是指   命名空间或类型

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.

下面的例子打印Ns1.Foo

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();
        }
    }
}

编辑:添加使用条款的一个命名空间,将让搜索命名空间的分层搜索当前的命名空间中完成之前完成。更改例子:

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();
        }
    }
}

Ns1.Foo.Foo2 将被打印出来。

编辑:改变例如

这篇关于命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆