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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With