简体   繁体   English

如何为 C# 配置默认的 VS Code 命名空间?

[英]How can I configure default VS Code namespaces for C#?

How can I configure default VS Code namespaces for C#?如何为 C# 配置默认的 VS Code 命名空间?

Eg I have the following folders structure:例如,我有以下文件夹结构:

gameplay/input/controllers/foo.cs

I created the foo.cs by right clicking the controllers folder and selecting New C# Class .我通过右键单击controllers文件夹并选择New C# Class创建了foo.cs And here is how the class looks by default without any changes from my side:以下是 class 默认情况下的外观,我这边没有任何更改:

namespace play.input.controllers
{
    public class foo
    {
        
    }
}

While I would expect the namespace in this case to be gameplay.input.controllers .虽然我希望在这种情况下的命名空间是gameplay.input.controllers I am confused why would VS Code change the gameplay to play ?我很困惑为什么 VS Code 会改变gameplay play How could I fix it?我该如何解决?

在此处输入图像描述 在此处输入图像描述

UPDATE更新

I can not find the RootNamespace in my project:我在我的项目中找不到RootNamespace

在此处输入图像描述

So, the provided answer has no value for me.所以,提供的答案对我来说没有价值。

You can set the default namespace of a project in Project Preferences->Application.您可以在 Project Preferences->Application 中设置项目的默认命名空间。 The string that is visible in the "Default Namespace" input box is used for new files in the root directory of the project. “默认命名空间”输入框中可见的字符串用于项目根目录中的新文件。 If you create items in subfolders, these will be included in the namespace by default.如果您在子文件夹中创建项目,这些项目将默认包含在命名空间中。

If you want to remove the application name ( play in your case), you can define the default namespace as empty.如果要删除应用程序名称(在您的情况下为play ),您可以将默认命名空间定义为空。

You can also edit the project file to include the RootNamespace tag, as follows:您还可以编辑项目文件以包含RootNamespace标记,如下所示:

  <PropertyGroup>
    <TargetFrameworks>net5.0;netcoreapp2.1</TargetFrameworks>
    <EnableDefaultItems>false</EnableDefaultItems>
....
    <RootNamespace>MyRootNamespace</RootNamespace>
....
    
  </PropertyGroup>

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

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