-->

Create a method call analyzer with Roslyn

2019-06-09 12:26发布

问题:

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