0

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!

Samorix
  • 307
  • 4
  • 17

1 Answers1

2

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

George Alexandria
  • 2,841
  • 2
  • 16
  • 24