繁体   English   中英

StyleCop.Analyzers 是否支持 C# 记录?

[英]Does StyleCop.Analyzers support C# records?

问题描述:

在带有StyleCop.Analyzers的项目中添加C#记录时,它会显示警告:

Warning AD0001  Analyzer 'StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName' threw an exception of type 'System.ArgumentException' with message 'Unhandled declaration kind: RecordDeclaration'.    

System.ArgumentException: Unhandled declaration kind: RecordDeclaration
       at StyleCop.Analyzers.Helpers.NamedTypeHelpers.GetNameOrIdentifier(MemberDeclarationSyntax member)
       at StyleCop.Analyzers.Helpers.FileNameHelpers.GetConventionalFileName(MemberDeclarationSyntax declaration, FileNamingConvention convention)
       at StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName.Analyzer.HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
       at StyleCop.Analyzers.AnalyzerExtensions.<>c__DisplayClass0_0.<RegisterSyntaxTreeAction>b__0(SyntaxTreeAnalysisContext c)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSyntaxTreeActionsCore>b__59_1(ValueTuple`2 data)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

它是StyleCop.Analyzers中的错误吗?

重现步骤:

  • 创建一个新的.Net6控制台项目(甚至是.Net5 )。

  • 在其中添加一条C#记录: public record Member(string FirstName, string LastName);

  • 安装 package StyleCop.Analyzers v1.1.118

  • 查看 Visual Studio 错误列表,您将看到警告。

谢谢@stuartd,您为我指明了正确的方向。

这确实是 Stylecop 1.1.X 中的一个错误

它在StyleCop.Analyzers.Unstable v1.2.0-beta.161中得到纠正。 但不幸的是,它还没有包含在稳定版本中。 所以我现在看到的解决方法是使用不稳定的版本

暂无
暂无

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

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