How to find code where a method is being invoked?


  • 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 Dan Eyassu Saturday, July 21, 2012 10:10 PM
    Saturday, July 21, 2012 10:09 PM

All replies

  • 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)



    Monday, July 23, 2012 5:55 PM
  • 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

    • Proposed as answer by Tobi_B Thursday, August 02, 2012 5:32 AM
    Friday, July 27, 2012 5:26 AM
  • Thanks, I get it now.
    Tuesday, September 18, 2012 7:00 PM