How to find code where a method is being invoked?
-
Saturday, July 21, 2012 10:09 PM
Is it possbile to search for and locate code where ANY class's method is being invoked, irrespective of where the method lives?
In the example below, I want to find the code where the foo.Do("Hello", 50); method is located.
class Foo { public Foo() {} public Do(string s, int n){ ... } } class Bar { public void SomeMethod(Foo foo) { foo.Do("Hello", 50); } }
- Edited by Danieleyassu Saturday, July 21, 2012 10:10 PM
All Replies
-
Monday, July 23, 2012 5:55 PM
Hi,
Method calls are parsed into a SyntaxNode of type InvocationExpressionSyntax. You can create a Syntax Walker that traverse the tree and aggregate the nodes of that type. Such walker could look similar to the following:
class InvocationWalker : SyntaxWalker { public readonly List<InvocationExpressionSyntax> InvocationExpressions = new List<InvocationExpressionSyntax>(); public override void VisitInvocationExpression(InvocationExpressionSyntax node) { InvocationExpressions.Add(node); base.VisitInvocationExpression(node); } }Thanks,
Mohammed
-
Friday, July 27, 2012 5:26 AMOwner
Mohammed's answer is one approach. However, if you are using the "Roslyn.Services.dll" (and add a "using Roslyn.Services;"), you will also find that the ISymbol type has an extension method named FindReferences that will do the searching for you and return the results.
Hope this helps.
-- Kevin Pilch-Bisson kevinpi@microsoft.com
- Proposed As Answer by Tobi_B Thursday, August 02, 2012 5:32 AM
-
Tuesday, September 18, 2012 7:00 PMThanks, I get it now.

