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/