不明确的类型名称

时间:2014-02-19 14:14:59

标签: c# types

我对类型的阴影可见性存在问题。我们假设以下代码:

namespace A {
    class B {
        int V1;
        class A {
            class B { }
            void Foo() {
                A.B b; 
                // "b" should be of the first type "B", 
                // but it actually points to A.B.A.B
                b.V1 = 1; //Compile error
            }
        }
    }
}

如何在声明“b”的地方声明类型“A.B”的变量(其中“A”应该是命名空间,而不是嵌套的类“A”)?

2 个答案:

答案 0 :(得分:11)

使用“global”关键字:

global::A.B b;

答案 1 :(得分:4)

您可以使用别名:

using ClassB = A.B;

现在ClassB指的是您想要的类型。

但是,我强烈建议您重新考虑您的班级名称,这样就不再是问题了。