Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get statements for IMethodSymbol?

I'm working on the Code Fix provider for .Net.

I'd like to check method internals, e.g. method statements from IMethodSymbol.

As an example, I have following code on input:

public void DoSomething(string input)
{
    if(input == null)
        throw new InvalidOperationException("!!!!");
}

On the code-fix side I have IMethodSymbol interface, and there are not ability to get method statements, internal nodes, etc. (I'd like to see 'if', condition into the 'if', exception raising, etc).

How can I get it?

like image 794
Manushin Igor Avatar asked Jan 06 '23 18:01

Manushin Igor


1 Answers

Use the DeclaringSyntaxReferences property to get the syntax tree defining the method.

Partial methods will have two nodes.

Methods defined in metadata (referenced assemblies) won't have any.

like image 122
SLaks Avatar answered Jan 14 '23 19:01

SLaks