Change Default Namespace when creating Class in Folder (Visual Studio)

后端 未结 5 386
一生所求
一生所求 2020-12-08 09:34

How can I change the default namespace used when you create a new class in Visual Studio?

Background: My solution has a project MyCompany.MyProject.Domain in which I

5条回答
  •  無奈伤痛
    2020-12-08 10:03

    Resharper has changed how they do this (as of VS2017).

    1. Add your folder.
    2. Add your first class.
    3. Manually alter the namespace to be what you want. (Usually that's removing the last piece from the namespace that represents the folder the class is in.)
    4. The namespace will now be underlined to say that it doesn't match the folder structure. Click on the the options to fix it, and select "Folder '...' do not introduce namespace".

    Now your folder is set to expect the namespace for all new classes. However, when you add a new class to the folder, it now complains that it's not the namespace that you set it to in step #3 above.

    So, you still need to manually alter the namespace, but at least resharper isn't trying to fight you anymore.

提交回复
热议问题