我创建了一个小dotnetstandard 2.1
解决方案中的库项目。我想测试一下使用可空引用类型 https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references。作为其中的一部分,我希望有适当的编译错误和警告。
太长了;
我想知道如何设置 CA1062 代码质量设置.editorconfig
正确。
图书馆计划
我添加了以下内容nuget
打包到项目中:
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Ardalis.GuardClauses" Version="1.4.2" />
</ItemGroup>
这包括各种代码分析包以及 Steve Smith 的好保护条款 https://github.com/ardalis/GuardClauses图书馆。
可空引用类型已启用<Nullable>enable</Nullable>
在项目中。
我有一个类,在现实世界中这将是一个很好的实现,它实际上做了一些事情:
using System;
using MyGuards;
namespace EditorConfigIssues
{
public static class TestEditorConfig
{
public static void TestMethod(MyParam input)
{
string x = input.Check;
Console.WriteLine(x);
}
}
public class MyParam
{
public MyParam(string check) => Check = check;
public string Check { get; }
}
}
警卫图书馆项目
在解决方案中,我添加了一个简单的 Guard 库,这是另一个dotnetstandard 2.1
项目。
using System;
namespace MyGuards
{
public static class FakeGuard
{
public static void Validate(object obj)
{
if (obj == null)
throw new ArgumentNullException();
}
}
}
注意:这不是 GuardClauses 库的竞争 - 只是用来与 editorconfig 进行对比!
.editorconfig
我添加了一个.editorconfig
通过以下诊断检查找到解决方案的根源:
dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
So with this in place, when I compile I get the following:
所以到目前为止一切都如我所料。我还没有使用任何守卫。
修复它 - 史蒂夫·史密斯的警卫图书馆
因此,让我们尝试实现 Stevie Smiths Guard Library 中的保护子句来消除错误。
所以我们添加以下内容TestEditorConfig.TestMethod
:
Guard.Against.Null(input, nameof(input));
并调整.editorconfig
with:
dotnet_code_quality.CA1062.null_check_validation_methods = Ardalis.GuardClauses.Guard.Against.Null
Excellent, all is good. The error has disappeared.
修复它 - 我自己的守卫库
但现在我想用我自己的守卫。所以我们更换了最初的警卫检查TestEditorConfig.TestMethod
with:
FakeGuard.Validate(input);
并替换 null_check_validation_methods.editorconfig
with:
dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate
The error is now back.
问题)
- 问题是,为了使用具有来自同一解决方案的守卫的项目,我需要什么?
- 为什么我会在错误窗口中收到有关其他 CA1062 的警告?
The keyword "dotnet_code_quality.CA1062.exclude_extension_method_this_parameter" is unknown
The keyword "dotnet_code_quality.CA1062.null_check_validation_methods" is unknown
我一直在查看这个链接MS 文档代码质量 https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019并尝试了 FakeGuard 的各种组合,包括:
- MyGuards
- MyGuards.FakeGuard
- 假卫士
- MyGuards.FakeGuard.Validate
- FakeGuard.验证
- Validate
奇怪的是,在不同的解决方案中,我没有收到任何关于错误窗口中的 CA1062 editorconfig 设置的投诉。在那里我一直无法得到null_check_validation_methods
工作 - 因此整合了这个解决方案。这已经困扰我一两个月了,但现在终于有精力把事情整合起来。
编辑器配置错误?
如果我将 FakeGuard 文件复制到 Library 项目,则错误消息就会消失。但为什么这在解决方案的不同项目中不起作用。