【问题标题】:Notify Visual Studio of code called through reflection通知 Visual Studio 通过反射调用的代码
【发布时间】:2020-03-09 05:40:07
【问题描述】:

上下文:

我工作的环境有很多通过外部代码反射调用或设置的“神奇”方法和字段。某些东西可能有一个属性,这意味着它将被设置为非默认值,但 Visual Studio 仍然看不到该方面,并且“有帮助”提供了警告。

由于这些是使用属性和专门命名的方法处理的,理想情况下,我想为 VS 提供额外的信息,以便它知道它被调用或设置,而无需我手动抑制每个警告。

我已经考虑编写一个 Roslyn 分析器,但据我所知,我只能添加额外的警告,不能修改现有的警告/引用计数。

示例:

[MyCmpGet] private Component comp

“字段永远不会被分配,并且总是有它的默认值 null”

但是由于注解,该字段是通过反射分配的。

[HarmonyPatch]
class Patch
{
     static void Postfix() {}
}

“未使用私人会员”
“0 篇参考文献”

但是方法是通过反射调用的,由于类上的注解,并且方法具有特定的名称。

问题:

让 Visual Studio 知道正在设置这些字段以及正在引用这些方法的最佳方法是什么?最好不需要我对每一个都进行手动操作,也不需要在示例代码中添加任何其他内容。

【问题讨论】:

    标签: c# visual-studio reflection roslyn


    【解决方案1】:

    您可以实现DiagnosticSuppressor 分析器。默认分析器模板应生成一个 nuget 包,您可以将其包含在您想要禁止显示此警告的所有项目中。

    以下是诊断抑制器最基本形式的示例:

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Diagnostics;
    using System.Collections.Immutable;
    
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public sealed class DiagnosticSuppressorForAssignmentWarnings : DiagnosticSuppressor {
        public SuppressionDescriptor SuppressionDescriptor => new SuppressionDescriptor(
            id: "SPR0001", // Id for this analyzer suppressor (You should come up with a unique name for yours)
            suppressedDiagnosticId: "CS0649", // The warning that we may want to suppress
            justification: "This is ok because it is assigned via reflection");
    
        public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions
            // You can pass in multiple suppression descriptors to have this suppress multiple types of warnings
            => ImmutableArray.Create(SuppressionDescriptor);
    
        public override void ReportSuppressions(SuppressionAnalysisContext context) {
            foreach (var diagnostic in context.ReportedDiagnostics) {
                // The parsed syntax tree of the file that this warning comes from
                var syntaxTree = diagnostic.Location.SourceTree;
                // The syntax Node that the warning was reported for
                var nodeWithWarning = syntaxTree.GetRoot().FindNode(diagnostic.Location.SourceSpan);
                // A semantic model that can answer questions like 'does this attribute inherit from this type' etc.
                var semanticModel = context.GetSemanticModel(syntaxTree);
    
                // You can do additional analysis of the source to ensure this is something 
                // that is semantically safe to suppress (like check for an attribute)
    
                context.ReportSuppression(Suppression.Create(SuppressionDescriptor, diagnostic));
            }
        }
    }
    
    

    有关如何编写分析器的更多文档,我会查看 this 博客文章或 this 文档

    【讨论】:

    • 这个作品应该单独工作,还是我还缺少其他作品?我可以让正常的分析器工作,但我无法让抑制工作 - 代码甚至似乎没有执行,我不知道为什么。
    • 您使用的是哪个版本的 Visual Studio?抑制分析器是一项新功能
    • 我正在使用 VS2019。我必须从模板项目更新 nuget 包,才能访问 Suppression 的东西,但我得到了它的工作和编译。 SupportedSuppressions 属性被多次调用(使用断点测试),但代码从未进入 ReportSuppressions 方法。
    • 我相信我终于弄明白了。使用 VSIX 测试分析器不适用于生成警告。手动将包安装到我的项目似乎可以按预期工作。
    • 如何将它安装到 CSharpCompilation 对象中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多