C# 在文件夹中创建类时更改默认命名空间 (Visual Studio)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1317901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Default Namespace when creating Class in Folder (Visual Studio)
提问by Alex
How can I change the default namespace used when you create a new class in Visual Studio?
如何更改在 Visual Studio 中创建新类时使用的默认命名空间?
Background: My solution has a project MyCompany.MyProject.Domain in which I have a folder "Model Base (Linq)" with a subfolder "Extensions" where I keep all partial class extensions.
背景:我的解决方案有一个项目 MyCompany.MyProject.Domain,其中我有一个文件夹“Model Base (Linq)”和一个子文件夹“Extensions”,我在其中保留了所有部分类扩展。
MyCompany.MyProject.Domain
+ Model Base (Linq)
+ Extensions
- Order.cs
- Trace.cs
When I create a new .cs file in there, the namespace gets set to MyCompany.MyProject.Domain.Model_Base\_\_Linq\_\_.Extensions, I only want it to be MyCompany.MyProject.Domain.Model though.
当我在那里创建一个新的 .cs 文件时,命名空间被设置为 MyCompany.MyProject.Domain.Model_Base\_\_Linq\_\_.Extensions,不过我只希望它是 MyCompany.MyProject.Domain.Model。
How can I change this default behavior?
如何更改此默认行为?
采纳答案by Marc Gravell
There are 4 schools of thought here:
这里有4种思想流派:
- edit it manually
- live with it
- change the class template (or export a second class template)
- create the file outside the sub-directory, then drag it in
- 手动编辑
- 忍受它
- 更改类模板(或导出第二个类模板)
- 在子目录外创建文件,然后将其拖入
I tend to use the 4th; it is quicker than editing, and less hassle than changing the templates
我倾向于使用第四个;它比编辑更快,比更改模板更省事
回答by Noon Silk
To change the default namespace:
要更改默认命名空间:
Right click on the project, go to properties and under the 'Application' tab there is a 'Default namespace' field.
右键单击项目,转到属性,然后在“应用程序”选项卡下有一个“默认命名空间”字段。
Note that this doesn't answer your exact question though, it depends on your folder. You basically you need to rename that folder to 'Model'.
请注意,这并不能回答您的确切问题,这取决于您的文件夹。您基本上需要将该文件夹重命名为“模型”。
回答by Adam
If you have ReSharper installed, right click on the folder in your solution you want to be excluded from namespaces and select properties. In the properties pane, select False
for Namespace Providerand that will make Visual Studio ignore the folder when generating namespaces in new files.
如果您安装了 ReSharper,请右键单击解决方案中要从命名空间中排除的文件夹并选择属性。在属性面板中,选择False
了命名空间提供者,这将让生成新文件的命名空间时,Visual Studio中忽略的文件夹。
回答by Steve Brownell
Resharper has changed how they do this (as of VS2017).
Resharper 已经改变了他们这样做的方式(从 VS2017 开始)。
- Add your folder.
- Add your first class.
- 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.)
- 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.
现在您的文件夹设置为所有新类的名称空间。但是,当您向文件夹添加新类时,它现在会抱怨它不是您在上面的第 3 步中设置的命名空间。
So, you still need to manually alter the namespace, but at least resharper isn't trying to fight you anymore.
因此,您仍然需要手动更改命名空间,但至少 resharper 不再试图与您抗争。
回答by Jan Paolo Go
If you already have one class under that folder with the desired namespace. You could take advantage of the Move type to ClassName.cs
light bulb suggestion.
如果该文件夹下已经有一个具有所需命名空间的类。您可以利用Move type to ClassName.cs
灯泡建议。
For example, you created Order.cs
with the correct namespace MyCompany.MyProject.Domain.Model
under the Extensions
folder. If you want to add Trace
class you do it like so:
例如,您在文件夹下创建Order.cs
了正确的命名空间。如果你想添加类,你可以这样做:MyCompany.MyProject.Domain.Model
Extensions
Trace
Ctrl + .
when the edit cursor is anywhere around the class Trace
words will show the suggestion.
Ctrl + .
当编辑光标位于class Trace
单词周围的任何位置时,将显示建议。
so... Ctrl + .
Down
Down
Enter
所以... Ctrl + .
Down
Down
Enter
Result:
结果: