.Net 4.0 中命名空间声明的错误或新约定
Visual Studio 2010,.Net 4.0
我正在生成一些域服务类,这就是 Visual Studio 包含命名空间的方式:
namespace MyCoolProject.Web.Services
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Columbus.Web.Data;
using Microsoft.ServiceModel.DomainServices.LinqToSql;
// Implements application logic using the JanDoetsDataContext context.
// TODO: Add your application logic to these methods or in additional methods.
// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class AnnouncementService : LinqToSqlDomainService<MydataDataContext>
{......................
此类所需的命名空间包含在命名空间声明内,换句话说, using bla bla bla 行位于MyCoolProject.Web.Services 命名空间声明不在外面。这是新约定还是vs2010的bug?
Visual Studio 2010, .Net 4.0
I'm generating some Domain Service Classes and this is how visual studio includes the namespaces:
namespace MyCoolProject.Web.Services
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Columbus.Web.Data;
using Microsoft.ServiceModel.DomainServices.LinqToSql;
// Implements application logic using the JanDoetsDataContext context.
// TODO: Add your application logic to these methods or in additional methods.
// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class AnnouncementService : LinqToSqlDomainService<MydataDataContext>
{......................
the namespaces needed by this class are included inside the namespace declaration, in other words the using bla bla bla lines are inside the MyCoolProject.Web.Services namespace declaration and not outside. Is this a new convention or a vs2010 bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自 .NET 1.0 以来,可以将
using
指令放置在命名空间内。这样做是为了避免命名空间污染,因为using
指令包含的类型仅在包含using
语句的命名空间内可见。因此,他们只是更改了代码生成器,以遵循建议的做法,将using
指令放在尽可能深的命名空间中。将using
指令放置在任何命名空间之外会执行全局根命名空间中的包含操作。查看 StyleCop 规则 SA1200 了解更多详细信息。
更新
答案有点不精确 -
using
指令的位置是 C# 的事情,因此它应该显示为“自 C# 1.0 以来是可能的” 而不是“自.NET 1.0以来是可能的”。Placing
using
directives inside a namespace is possible since .NET 1.0. This is done to avoid namespace polution because the types include by theusing
directives are only visible inside the namespace containing theusing
statements. So they just changed the code generator to adhere to the recommended practice to putusing
directives in the deepest possible namespace. Placing the theusing
directives outside any namespace performs the include in the global root namespace.Have a look at StyleCop rule SA1200 for some more details.
UPDATE
The answer is a bit unprecise - the placement of the
using
directive is a C# thing and therefore it should read "is possible since C# 1.0" instead of "is possible since .NET 1.0".将 using 语句放置在命名空间内(基于 StyleCop 规则)一直是惯例,但 Visual Studio 中的默认模板将它们放在命名空间之外,因此这已成为最常见的用法。
自动生成的文件将 using 语句放置在命名空间内有一个很好的理由:它会影响引用的解析顺序。如果您在 MyCoolProject.Web 命名空间中定义的类与 Columbus.Web.Data 中使用的类共享名称,则该类的用法将解析为 MyCoolProject.Web.Class(如果使用在外部)或 Columbus.Web。 Data.Class(如果使用在内部)。因此,通过将 using 语句保留在命名空间内,自动生成的代码对于您可能在项目中所做的更改更加稳健。
这不会编译(因为 MyCoolProject.Web.Console 类没有定义 WriteLine):
而这将:
It has always been the convention to place using statements inside namespaces (based on StyleCop rules), but the default template in Visual Studio has them outside so that has become the most common usage.
There is a good reason why the auto-generated files place the using statements inside the namespace though: It affects the order that references are resolved. If you defined a class in the MyCoolProject.Web namespace that shared a name with a class used in Columbus.Web.Data then the usage of that class will resolve to MyCoolProject.Web.Class if the usings are outside, or Columbus.Web.Data.Class if the usings are inside. So, by keeping the using statements inside the namespace, the auto-generated code is more robust to changes that you may make in the project.
This won't compile (because the MyCoolProject.Web.Console class does not define WriteLine):
Whereas this will:
将
using
指令放入命名空间内是 Microsoft 内部代码风格约定的一部分(由 StyleCop 强制执行)。就我个人而言,我无法忍受,但事实就是如此。我认为我说得对,它真正能产生影响的唯一情况是,如果您在同一个代码文件中定义了多个类(在不同的命名空间中!),这被广泛认为禁忌。
我相信,自 C# 1.0 以来,它一直是可接受的语法。
Putting
using
directives inside thenamespace
is part of the internal Microsoft code style convention (as enforced by StyleCop). Personally I can't stand it, but it is what it is. I think I'm right in saying that the only circumstances under which it can actually make a difference are if you have multiple classes (in different namespaces!) defined in the same code file, which is widely considered a no-no.It's been acceptable syntax since C# 1.0, I believe.
我不确定这是否是一个根本原因,但请注意,至少在 .NET 3.5 中,如果您在部分类中的命名空间块之外声明 using 块,则存在一个错误,该错误会导致 Linq to SQL 无法工作你的数据上下文。
I'm not sure this is much of an underlying reason, but beware, at least in .NET 3.5, there's a bug that causes Linq to SQL not to work if you declare your using block outside of your namespace block in your partial class for your DataContext.