Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find property "invocations" with Roslyn

I'm trying to build up a call graph of C# methods and properties. This essentially means that I search the project for MethodDeclarationSyntax and PropertyDeclarationSyntax nodes. I then build connections between these nodes by looking for method invocations via:

SyntaxNode node = ...; //Some syntax node
var methodInvocations = node.DescendantNodesAndSelf().OfType<InvocationExpressionSyntax>();
//Process these method invocations

Is there a similar method or recommended way to find all property "invocations" as well? I believe the C# compiler breaks properties out into Getter and Setter functions on compilation.

What's the best way to detect the usage of properties with Roslyn?

like image 501
JoshVarty Avatar asked Feb 17 '14 23:02

JoshVarty


1 Answers

The Roslyn model follows source, not IL and so the individual calls to the get and set methods are not represented.

In order to do this, you'll need to find all of the MemberAccessExpression and IdentifierNameSyntax nodes and call GetSymbolInfo to see if they refer to the property.

Alternatively, you should consider bumping up a level to use the workspace model and call the FindReferences API instead.

like image 196
Kevin Pilch Avatar answered Nov 19 '22 15:11

Kevin Pilch