C# 命名空间和子命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/768905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Namespace and Sub Namespaces
提问by CodeLikeBeaker
Is there a way to use a namespace and then have it automatically use all sub namespaces?
有没有办法使用命名空间然后让它自动使用所有子命名空间?
Example:
例子:
namespace Root.Account
{
//code goes here
}
namespace Root.Orders
{
//code goes here
}
//New File:
using Root;
In order for me to use the code in Root.Account, I would need to add using Root.Account to my code.
为了让我使用 Root.Account 中的代码,我需要将 using Root.Account 添加到我的代码中。
I would like to be able to just say using Root and have it pick up any sub namespace classes for use.
我希望能够只说使用 Root 并让它选择任何子命名空间类以供使用。
If this makes sense, is this possible?
如果这是有道理的,这可能吗?
Thanks
谢谢
采纳答案by Jon Skeet
No, there's nothing working that way downthe tree. However, you don't need to include using directives to go upthe tree. In other words, if you use:
没有,没有什么工作这种方式下的树。但是,您不需要包含 using 指令来上树。换句话说,如果您使用:
namespace Root.Orders
{
// Code
}
then any types in Root
are already visible.
那么任何类型Root
都已经可见。
回答by J. Steen
No, not really. =)
不,不是真的。=)
回答by Daniel Brückner
.NET/C# does not support this form of namespace using.
.NET/C# 不支持这种形式的命名空间使用。
回答by Anton Gogolev
No, using
directive in C# does not allowthis:
Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.
创建 using 指令以使用命名空间中的类型,而无需指定命名空间。using 指令不允许您访问嵌套在您指定的命名空间中的任何命名空间。
VB.NET, however, supportssomewhat closer behavior with Imports
statement:
然而,VB.NET支持更接近于Imports
语句的行为:
The scope of the elements made available by an Imports statement depends on how specific you are when using the Imports statement. For example, if only a namespace is specified, all uniquely named members of that namespace, and members of modules within that namespace, are available without qualification. If both a namespace and the name of an element of that namespace are specified, only the members of that element are available without qualification.
Imports 语句提供的元素范围取决于您在使用 Imports 语句时的具体程度。例如,如果只指定了一个命名空间,则该命名空间的所有唯一命名的成员以及该命名空间内的模块成员都可以不加限定地使用。如果同时指定了命名空间和该命名空间的元素名称,则只有该元素的成员可用而无需限定。
That is, if you say
也就是说,如果你说
Imports Root
then you'll be able to just say Orders.Whatever
.
那么你就可以说Orders.Whatever
。
回答by Will Eddins
From the way you stated the original question, it sounds like you may or may not realize that from just using:
从您陈述原始问题的方式来看,听起来您可能会或可能不会从仅使用以下内容中意识到这一点:
using Root;
The way you clarify your code later on is:
稍后澄清代码的方式是:
Account.Class obj = new Account.Class();
Orders.Class obj = new Orders.Class();
You don't HAVE to have using Root.Account to access the code, and the same with Root.Orders. Other than that, no way to auto-recursively use namespaces. I think this would lead to abuse with using System;
您不必使用 Root.Account 来访问代码,Root.Orders 也是如此。除此之外,没有办法自动递归使用命名空间。我认为这会导致滥用 System;
回答by parijat mishra
namespace like a container of objects. They may contain union, structure, classes, delegate and interface. The main importance of namespace in Asp.net C# is for creating a hieratical organization of program.
命名空间就像一个对象的容器。它们可能包含联合、结构、类、委托和接口。命名空间在 Asp.net C# 中的主要重要性在于创建程序的分层组织。
learn more for blog...http://asp-net-by-parijat.blogspot.in/2015/08/what-is-namespace-in-c-need-of.html
了解更多博客... http://asp-net-by-parijat.blogspot.in/2015/08/what-is-namespace-in-c-need-of.html