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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top