简体   繁体   English

如何使公司名称进入命名空间?

[英]How to get the company name into the namespace?

My environment: 我的环境:

  • Microsoft Visual Studio 2010 C# Microsoft Visual Studio 2010 C#

I've read many articles and comments about, how I should organize my namespaces. 我已经阅读了许多有关如何组织命名空间的文章和评论。

http://msdn.microsoft.com/en-us/library/ms229026 http://msdn.microsoft.com/en-us/library/ms229026

So my question is quite simple. 所以我的问题很简单。 How do I get the company and project name automatically in my namespace? 如何在我的名称空间中自动获取公司和项目名称?

If I create subfolders in my project, so I get the following pattern: 如果在项目中创建子文件夹,则会得到以下模式:

<Project>.<Folder>.<Subfolder>.<..>
  • Solution1: 解决方案1:

    If you go to the Properties window of your Project , you can set the Default namespace (including the Company Name) and also the Assembly name in the Application tab. 如果转到“ Project的“ Properties窗口,则可以在“ Application选项卡中设置Default namespace (包括公司名称)以及Application Assembly name Below is a screenshot of this: 下面是此屏幕截图:

在此处输入图片说明

Now, after setting the default namespace, every time you add a new class to this project, it gets the above set namespace by default. 现在,在设置默认名称空间后,每次向该项目添加新类时,默认情况下它将获得上述设置的名称空间。

  • Solution2: 解决方案2:

Another solution is, every time you add a new project to your VS Solution, in the Add New Project template dialog, give the new project Name equal to CompanyName.ProjectName . 另一个解决方案是,每次将新项目添加到VS解决方案时,在“ Add New Project模板对话框中,将新项目的NameCompanyName.ProjectName This way your new project's Default namespace would include Company Name always (CompanyName.ProjectName) as shown in the Add New Project dialog screenshot below: 这样,新项目的Default namespace将始终包括Company Name (CompanyName.ProjectName),如下面的“添加新项目”对话框的屏幕截图所示: 在此处输入图片说明

VS will by default use your folder structure for namespaces. VS 默认将您的文件夹结构用于名称空间。
But you can always simply edit the code to change your namespace(s). 但是,您始终可以简单地编辑代码来更改您的名称空间。

And under Project Properties, Application you will find a setting for the Default namespace , which is used as the root for all other (folder-baed) namespaces. Project Properties(应用程序属性)下的Application(应用程序)您将找到Default命名空间的设置,该设置用作所有其他(基于文件夹的)命名空间的根。 This is the place where you would normally enter your Company name. 在此通常可以输入公司名称。

You do not need to add CompanyName as a name for the project as @Pravin Pawar said. 您不需要像@Pravin Pawar所说的那样将CompanyName添加为项目的名称。 If you ask me I won't add like that. 如果您问我,我不会这样补充。

There is a option under Project Properties called DefaultNamespace (Application tab) where you can extend your namespace with company name. 项目属性下有一个名为DefaultNamespace (应用程序选项卡)的选项,您可以在其中使用公司名称扩展名称空间。

My preference for most projects is to use CompanyName.Project as the default namespace. 对于大多数项目,我更喜欢使用CompanyName.Project作为默认名称空间。

You can use Pravian's way for new solution. 您可以使用Pravian的方法获得新的解决方案。 But for existed projects you should go to project properties in the Application tab to change Default namespace: . 但是对于现有项目,您应该转到“ Application选项卡中的项目属性以更改“ Default namespace:

When you create a new project, You specify the name of the project like CompanyName.ProjectName & then when you add new project to the same solution they follow the CompanyName.ProjectName namespace like 创建新项目时,请指定项目名称,例如CompanyName.ProjectName ,然后在将新项目添加到同一解决方案时,它们遵循CompanyName.ProjectName命名空间,例如

CompanyName.ProjectName.BAL
CompanyName.ProjectName.DAL
CompanyName.ProjectName.Client 

您可以在项目的属性中指定默认的根名称空间-右键单击Visual Studio中的项目文件,单击属性,然后在“应用程序”选项卡中设置默认名称空间以包括公司名称。

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

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