Reputation: 55
I'm trying to learn how to use Roslyn and I keep hitting roadblocks that take a lot of thought to figure out how to get around. At the moment, focusing on experimenting to understand what's possible.
void Method1(){}
void Method2()
{
Method1();
}
void Method3()
{
Method2();
}
If I want to check if Method2 calls Method1, It's easy because I just look at it's syntax tree. As I understand it, If I'm looking at Method3 though and I want to find Method2, I should use the semantic tree because Method2 may be in a different file/namespace/etc.
My question is if I have only the IMethodSymbol of Method2, is there any way to find out if Method2 calls Method1?
Thanks for any help
Upvotes: 5
Views: 1440
Reputation: 244757
If I understand you correctly, you know how to get from MethodDeclarationSyntax
for Method3
to IMethodSymbol
for Method2
and from MethodDeclarationSyntax
for Method2
to Method1
, but you don't know how to get from IMethodSymbol
for Method2
to MethodDeclarationSyntax
to Method2
.
To do that, you can use DeclaringSyntaxReferences
:
var method2Syntax = symbol.DeclaringSyntaxReferences.Single().GetSyntax();
Upvotes: 5