Create a method call analyzer with Roslyn

纵饮孤独 提交于 2019-12-10 11:53:13

问题


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?

Thanks in advance!


回答1:


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



来源:https://stackoverflow.com/questions/44829246/create-a-method-call-analyzer-with-roslyn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!