C# 创建一个Roslyn分析仪来检测;睡眠();.cs文件中的方法调用

C# 创建一个Roslyn分析仪来检测;睡眠();.cs文件中的方法调用,c#,roslyn,roslyn-code-analysis,C#,Roslyn,Roslyn Code Analysis,我问这个问题是为了延续这一点 我想创建一个roslyn分析器来检测.cs文件中sleep方法的使用情况。有人能帮我更正代码吗 using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.C

我问这个问题是为了延续这一点

我想创建一个roslyn分析器来检测.cs文件中sleep方法的使用情况。有人能帮我更正代码吗

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace SleepCustomRule
{
 [DiagnosticAnalyzer(LanguageNames.CSharp)]
public class SleepCustomRuleAnalyzer : DiagnosticAnalyzer
{
    public const string DiagnosticId = "SleepCheck";
    private const string Title = "Sleep function use in forbidden";
    private const string MessageFormat = "Remove this usage of sleep function";
    private const string Description = "Sleep function forbidden";
    private const string Category = "Usage";

    private static readonly ISet<string> Bannedfunctions = ImmutableHashSet.Create("Sleep");

    private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, 
        DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);


    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);


    public override void Initialize(AnalysisContext context)
    {
        context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.GenericName);
    }


    private static void AnalyzeNode(SymbolAnalysisContext context)
    {

        var tree = CSharpSyntaxTree.ParseText(@"
public class Sample
{
public string FooProperty {get; set;}
public void FooMethod()
{
}
 }");

        var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
        var compilation = CSharpCompilation.Create("MyCompilation",
            syntaxTrees: new[] { tree }, references: new[] { mscorlib });
        var semanticModel = compilation.GetSemanticModel(tree);

        var methods = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();

        foreach (var node in methods)
        {
            if (node != null)
            {
                // node – is your current syntax node
                // semanticalModel – is your semantical model
                ISymbol symbol = semanticModel.GetSymbolInfo(node).Symbol ?? semanticModel.GetDeclaredSymbol(node);
                if (symbol.Kind == SymbolKind.Method)
                {
                    string methodName = "sleep";
                    if ((symbol as IMethodSymbol).Name.ToLower() == methodName)
                    {
                        // you find your method
                        var diagnostic = Diagnostic.Create(Rule, symbol.Locations[0], symbol.Name);
                        context.ReportDiagnostic(Diagnostic.Create(Rule, node.Identifier.GetLocation()));

                    }
                }
            }
        }

    }

}
}
2-在中组装

var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
3-此外,我想从给定的文件中检测sleep方法的使用,而不是从我创建的var树中


提前非常感谢

AnalyzeNode
方法中使用
context.Node
。另外,使用
context.RegisterSyntaxNodeAction(AnalyzeNode,SyntaxKind.InvocationExpression)您可能需要阅读谢谢!我在1中遇到的错误是:CS0411无法从用法推断方法“AnalysisContext.RegisterSyntaxNodeAction(Action,params TLanguageKindEnum[])的类型参数。请尝试显式指定类型参数。@KrisVandermotten的答案正确,请更改寄存器,如果在更改后出现错误,则可能在某个地方缺少CSharpLanguage属性,请参阅默认分析器。您解决过这个问题吗?在
AnalyzeNode
方法中使用
上下文.Node
。另外,使用
context.RegisterSyntaxNodeAction(AnalyzeNode,SyntaxKind.InvocationExpression)您可能需要阅读谢谢!我在1中遇到的错误是:CS0411无法从用法推断方法“AnalysisContext.RegisterSyntaxNodeAction(Action,params TLanguageKindEnum[])的类型参数。请尝试显式指定类型参数。@KrisVandermotten回答正确,请更改您的寄存器,如果您的错误是在更改后发生的,您可能在某个地方缺少CSharpLanguage属性,请参阅默认分析器。您解决过这个问题吗?
var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);