Visual Studio C#编译器
warns about accidentally assigning a variable to itself,但此警告不适用于C#属性,仅适用于变量.如
this other question所述.
但是,如果我为自己分配一个属性,我真的会喜欢类似的东西,可以在编译时警告我.
我目前正在使用Visual Studio 2013,但如果解决方案至少在Visual Studio 2015中有效,我很好.此外,我不使用像ReSharper或CodeRush这样的第三方商业插件,所以我更倾向于不使用的解决方案涉及购买东西,但我愿意接受建议.
你知道我怎么能做到这一点?
背景:
我已经习惯了使用readonly public “inspection” properties存储接收到的依赖项的constructor dependency injection模式.
例如,假设一个依赖于ILogger实现的类Foo.记录器实例在构造函数中提供给类,构造函数检查空值并将依赖项存储在名为Logger的实例属性中:
public class Foo { public ILogger Logger { get; private set; } public Foo(ILogger logger) { if(logger == null) throw new ArgumentNullException("logger"); this.Logger = logger; } }
但是,我经常输入错误,将属性分配给自己而不是传递给构造函数的参数.
public class Foo { public ILogger Logger { get; private set; } public Foo(ILogger logger) { if(logger == null) throw new ArgumentNullException("logger"); this.Logger = Logger; // <-- This is wrong. Property assigned to itself. } }
当然,我总是在测试和调试时总是抓住这些错误,但它已经咬过我几次了,我不想再浪费时间在这样一个愚蠢的错误中.
有什么建议?
解决方法
这是VS2015实时代码分析器的完美情况.您可以编写一个基本分析器来检查是否将属性分配给自身并创建错误.
这是一个非常好的教程,我很久以前就帮助你开始学习如何写一个,他们不是很难做到:“C# and Visual Basic – Use Roslyn to Write a Live Code Analyzer for Your API”
更新:我有一些空闲时间所以我写了一个分析器,它做到了.
[DiagnosticAnalyzer(LanguageNames.CSharp)] public class SelfAssignmentAnalyzer : DiagnosticAnalyzer { public const string DiagnosticId = "SelfAssignment"; private static readonly LocalizableString Title = "Do not do self assignment"; private static readonly LocalizableString MessageFormat = "The variable '{0}' is assigned to itself"; private static readonly LocalizableString Description = "A variable assignment to itself is likely an indication of a larger error."; private const string Category = "Correctness"; private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId,Title,MessageFormat,Category,DiagnosticSeverity.Warning,isEnabledByDefault: true,description: Description); public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } } public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(AnalyzeNode,SyntaxKind.SimpleAssignmentExpression); } private void AnalyzeNode(SyntaxNodeAnalysisContext context) { var assignmentExpr = (AssignmentExpressionSyntax)context.Node; var right = context.SemanticModel.GetSymbolInfo(assignmentExpr.Right); var left = context.SemanticModel.GetSymbolInfo(assignmentExpr.Left); if (!right.Equals(left)) return; var diagnostic = Diagnostic.Create(Rule,assignmentExpr.GetLocation(),assignmentExpr.Left.ToString()); context.ReportDiagnostic(diagnostic); } }
您可以进行优化,可以在不调用GetSymbolInfo的情况下排除情况(例如,检查左侧和右侧的文本以查看它们是否匹配)但我将其作为练习留给您.
编辑:
Visual Studio 2015中的分析器: