使用dotnet-format更改为c#10中的文件划分的名称空间

发布于 2025-01-23 20:02:32 字数 1369 浏览 4 评论 0原文

我想将文件格式化为“文件划分的名称空间”。我只想做出改变。

我的.editorConfig仅包含以下内容:

[*.cs]
csharp_style_namespace_declarations = file_scoped:warning     # config for IDE0161

测试文件foo.cs

namespace MyNamespace
{

    public class Foo
    {

        public Foo(int someInt, string someString)
        {
            _someInt    = someInt;                    // aligned
            _someString = someString;                 // aligned
        }

        private readonly int _someInt;
        private readonly string _someString;

    }
}

我运行此:

dotnet format MyProject.csproj --severity info --diagnostics=IDE0161 --include=Foo.cs

结果:

namespace MyNamespace;

public class Foo
{

    public Foo(int someInt, string someString)
    {
        _someInt = someInt;                     // not aligned
        _someString = someString;               // not aligned
    }

    private readonly int _someInt;
    private readonly string _someString;

}

如图所示,对齐方式更改。还有其他各个地方也改变了对齐方式。

我只想运行一个分析仪/修复程序 - iDE0161。我不希望格式命令做其他任何事情。

我该怎么做?

(请注意,如果我在VSCODE中运行代码修复,则可以使用。它仅通过dotnet格式失败。)

I want to format a file to "file-scoped namespaces". I want to make that change only.

My .editorconfig contains this only:

[*.cs]
csharp_style_namespace_declarations = file_scoped:warning     # config for IDE0161

A test file Foo.cs:

namespace MyNamespace
{

    public class Foo
    {

        public Foo(int someInt, string someString)
        {
            _someInt    = someInt;                    // aligned
            _someString = someString;                 // aligned
        }

        private readonly int _someInt;
        private readonly string _someString;

    }
}

I run this:

dotnet format MyProject.csproj --severity info --diagnostics=IDE0161 --include=Foo.cs

The result:

namespace MyNamespace;

public class Foo
{

    public Foo(int someInt, string someString)
    {
        _someInt = someInt;                     // not aligned
        _someString = someString;               // not aligned
    }

    private readonly int _someInt;
    private readonly string _someString;

}

The alignment is changed as shown. There are various other places where alignment is changed too.

I only want to run one analyser/fixer - IDE0161. I don't want the format command to do anything else.

How do I do that?

(Note if I run the code fix in vscode, it works. It's only via dotnet format that it fails.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文