c# - 如何强制执行 C#/.NET 的代码样式,例如 IDE0058、IDE0059 和 IDE0060?

标签 c# .net gitlab dotnet-cli

我正在研究强制执行代码样式,并开始实现 IDE0058 , IDE0059IDE0060在我的 .editorconfig 中,如下所示:

csharp_style_unused_value_expression_statement_preference = discard_variable:error
csharp_style_unused_value_assignment_preference = discard_variable:error
dotnet_code_quality_unused_parameters = all:error

当我在 Windows 上的 Visual Studio 2022 中运行构建时,我确实遇到了错误,尽管构建仍然成功。 但是,当我从命令行运行 dotnet build 时,我没有收到任何错误,并且当我搜索样式代码时,我找不到任何提及它们的内容。

同事在他的macbook上测试了一下,mac版的visual studio没有报错。命令行也没有。

在我们的 gitlab 管道中,我们还运行了 dotnet build,它也顺利通过,没有任何错误。

在所有环境中,我们都使用 .NET 6.0。

是否可以使用 .editorconfig 强制执行这些代码样式?如果是这样,怎么办?

如果没有,如何强制执行代码样式?

最佳答案

将以下行添加到 csproj 文件

<PropertyGroup>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
    <AnalysisLevel>6.0-recommended</AnalysisLevel>
</PropertyGroup>

然后在 editorconfig 中除了现有规则之外添加

dotnet_diagnostic.IDE0058.severity = error
dotnet_diagnostic.IDE0059.severity = error
dotnet_diagnostic.IDE0060.severity = error

关于c# - 如何强制执行 C#/.NET 的代码样式,例如 IDE0058、IDE0059 和 IDE0060?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70517332/

相关文章:

c# - 如何在 Azure Service Fabric ASP.NET Core 有状态服务中获取 PartitionInfo?

c# - 检查服务器路径是否可用作 C# 中的文件共享

.net - 在 .Net 中解析自定义 JSON 对象?

linux - 客户端 git hooks 在服务器中的位置在哪里

c# - 在 Azure 函数中找不到类型或命名空间名称 'SendGridClient'

c# - 在 .ashx 文件中创建了一个 session 并想在 .aspx 页面中访问但我发现了空值

c# - DataGridView 为列中的某些单元格着色

c# - ListBox Groupstyle 显示 : How to design a group name?

docker - 在Gitlab CI/CD中,如何提交和发布正在运行我们阶段的docker容器

ubuntu - 为什么我在尝试仅从我的 Ubuntu 访问 gitlab 时突然得到 ERR_TIMED_OUT?