简体   繁体   中英

How to get the company name into the namespace?

My environment:

  • 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

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:

    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. 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:

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 . 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: 在此处输入图片说明

VS will by default use your folder structure for namespaces.
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. 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. 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.

My preference for most projects is to use CompanyName.Project as the default namespace.

You can use Pravian's way for new solution. But for existed projects you should go to project properties in the Application tab to change 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.BAL
CompanyName.ProjectName.DAL
CompanyName.ProjectName.Client 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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