使用 Roslyn 创建方法调用分析器 [英] Create a method call analyzer with Roslyn

查看:70
本文介绍了使用 Roslyn 创建方法调用分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析 .cs 文件以查找特定方法.例如,一旦调用了名为X"的方法,分析器就应该检测到它.

I need to parse a .cs file to look for a particular method. For instance, once the method named "X" is called, the analyzer should detect it.

如何检测这个特定节点是一个方法?

How can detect that this particular node is a method?

提前致谢!

推荐答案

如果你有一个语法节点和语义模型,你可以试试这个:

If you have a syntax node and semantic model for it you can try this:

// 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)
{
    // methodName – is a method's name that you are looking
    if((symbol as IMethodSymbol).Name == methodName)
    {
        // you find your method
    }
}

另外,不使用语义模型也可以确定当前语法节点是你的方法,但比上面的方法要难一些

Also, you can determine that the current syntax node is your method without using the semantic model but it's a little harder than the way is above

这篇关于使用 Roslyn 创建方法调用分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆