.net - .Net 4.0 中命名空间声明的错误或新约定

标签 .net namespaces conventions

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 错误?

最佳答案

从 .NET 1.0 开始,可以将 using 指令放在命名空间中。这样做是为了避免命名空间污染,因为 using 指令包含的类型仅在包含 using 语句的命名空间内可见。因此,他们只是更改了代码生成器,以遵循推荐的做法,将 using 指令放在尽可能深的命名空间中。将 using 指令放在任何命名空间之外会执行包含在全局根命名空间中。

查看 StyleCop 规则 SA1200了解更多详情。

更新

答案有点不精确 - using 指令的放置是 C# 的事情,因此它应该读作 “自 C# 1.0 以来是可能的” 而不是 “自 .NET 1.0 以来是可能的”

关于.net - .Net 4.0 中命名空间声明的错误或新约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236379/

相关文章:

c# - Stylecop 告诉我添加这个关键字,但它是多余的 - 对性能有影响吗?

c# - 静态枚举类成员

c# - 如何获取数组中方法中使用的泛型类型?

c# - 如何选择一个好的命名空间名称? [语言]

Javascript - 命名空间与闭包之间的区别?

javascript - 如何将字符串变量转换为命名空间对象

node.js - node.js 中 app.js、index.js 和 server.js 的约定?

c# - 如何将属性传递给另一个命名空间中的方法

standards - MIME 类型的约定、标准或限制?

coding-style - 用 "/>"结束 Coldfusion 自闭合标签是一种好习惯吗?