【发布时间】:2019-09-26 09:28:39
【问题描述】:
我觉得我在这里遗漏了一些明显的东西,但是这个问题已经困扰了我一段时间。
我在几个不同的嵌套命名空间中定义了一些类:
namespace Awesome.Example {
namespace V1 {
public class PopTart {
public bool PoppinUp {get; set;}
public bool Hot {get; set;}
}
}
namespace V2 {
public class PopTart {
public bool PoppinUp {get; set;}
public bool SoCool {get; set;}
public bool AreHot {get; set;}
}
}
}
但如果我尝试使用包含来简化声明,IDE (VS) 会抱怨我需要完全限定类。
例如:
using Awesome.Example;
public class Main {
public V1.PopTart Strawberry {get; set;}
public V2.PopTart Blueberry {get; set;}
}
这会产生两个错误:
The type or namespace name 'V1' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'V2' could not be found (are you missing a using directive or an assembly reference?)
我不明白这是为什么。谁能解释如何完成我所追求的?
【问题讨论】:
-
请指定属性名:public
V1.PopTart PROPERTY_NAME {get; set;} -
你不能那样做。如果在
Awesome.Example中定义了Main,或者Awesome中的Main可以声明public Example.V1.PopTart A { get; set; },则像V1.PopTart这样的部分命名空间将起作用。但是Main必须与这些类有一些共同的“祖先”命名空间。否则,它必须全有或全无。这可能是为了使名称解析更易于管理。 -
谢谢你,这大大澄清了事情。我不知道共同祖先是必要的。我以为提供“使用”就足够了。
标签: c# .net namespaces