简体   繁体   English

为多个名称空间使用相同的别名

[英]Using same alias for multiple namespaces

Is it possible to somehow use the same using alias for multiple namespaces, for which I know they don't have overlapping class names, in C#? 是否有可能以某种方式使用相同的using别名多个命名空间,为此,我知道他们没有重叠的类名称,在C#中?
For example if I could do something like this: 例如,如果我可以做这样的事情:

using NSP = namespace1.namespace2;
using NSP = namespace1.namespace3;  

namespace2 and namespace3 don't have classes with the same name, so there're no worries about ambiguous class names, and it would be more convenient for me to write: namespace2namespace3没有具有相同名称的类,因此不必担心模糊的类名,并且编写它会更方便:

NSP.Class1 obj1 = new NSP.Class1();

than

NSP.namespace2.Class1 obj1 = new NSP.namespace2.Class1();

in case I use 以防我使用

using NSP = namespace1;
using NSP = namespace1.namespace2;
using NSP = namespace1.namespace3;  

You can't do that first of all. 你首先不能做到这一点。 Compiler doesn't let you define same aliases for two different namespaces. 编译器不允许您为两个不同的命名空间定义相同的别名。 That gives compiler time error. 这给编译器时间错误。

namespace2 and namespace3 don't have classes with the same name, so there're no worries about ambiguous class names, namespace2和namespace3没有具有相同名称的类,因此不必担心模糊的类名,

It doesn't matter they have same named classes or not, compiler doesn't let you do that. 它们具有相同的命名类并不重要,编译器不允许您这样做。

You can't use same alias name again. 您不能再使用相同的别名。 you will get the following compiler error 您将收到以下编译器错误

The using alias 'xxx' appeared previously in this namespace 使用别名'xxx'之前出现在此命名空间中

No you can't, even the two namespaces don't have classes with the same name. 不,你不能,即使这两个名称空间也没有同名的类。 You will get thsi error : 你会得到这个错误:

The using alias 'NSP' appeared previously in this namespace. 使用别名'NSP'先前出现在此命名空间中。

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

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