4

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?

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
  • When you mention that "On the code-fix side I have IMethodSymbol interface", how did you end up with this method symbol ? Don't you have any underlying syntax nodes (such as a [MethodDeclarationSyntax](http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp/Syntax/MethodDeclarationSyntax.cs) from which you could get the method statements nodes from ? – Phil Gref Jul 10 '16 at 19:36
  • @PhilGref In my case, I need to register for the semantic node so that I can identify whether the method is ultimately overriding a specific method of a specific type. Then if it is, I need to parse the syntax of the body and see how one of the parameters is treated. – jnm2 Feb 02 '17 at 22:47

1 Answers1

9

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964