Using Roslyn to find all references

Here is a simple code using Roslyn to find all references for a given function.

We need to add following NuGet packages :

  1. Microsoft.Build.Locator
  2. Microsoft.CodeAnalysis.Workspaces.MSBuild
  3. Microsoft.Build
  4. Microsoft.Build.Tasks.Core
  5. Microsoft.CodeAnalysis.CSharp.Workspaces

using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Roslyn
    class Program
        static void Main(string[] args)
            var workspace = MSBuildWorkspace.Create();

            var solutionTask = workspace.OpenSolutionAsync(@"D:\Projects\John\PwC.SDK\PwC.SDK\PwC.SDK.sln");
            var solution = solutionTask.Result;

            var project = solution.Projects.SingleOrDefault(p => p.Name == "PwC.SDK.Web");

            var compilationTask = project.GetCompilationAsync();
            var compilation = compilationTask.Result;

            //to search for functions within our solution
            var myFunction = compilation.GetSymbolsWithName(s => s.StartsWith("TileMetrics"), SymbolFilter.All).FirstOrDefault();

            //to search for all reference of this above function without solution
            var callerTask2 = SymbolFinder.FindReferencesAsync(myFunction, solution);

            // to search for function within our solution and all referenced dlls
            var externalFunctionsTask = SymbolFinder.FindDeclarationsAsync(project, "MapRoute", true);
            var externalFunctions = externalFunctionsTask.Result;


            foreach (var externalFunction in externalFunctions)
                var callerTask = SymbolFinder.FindReferencesAsync(externalFunction, solution);
                var callers = callerTask.Result;

                Console.WriteLine($"Function name is {externalFunction.ToDisplayString()}");
                foreach (var referenced in callers)
                    Console.WriteLine($"Number of references {referenced.Definition.Name} {referenced.Locations.Count()}");
                    foreach (var location in referenced.Locations)
                        Console.WriteLine($"FileName (line#) {location.Location.SourceTree.FilePath} ({location.Location.GetLineSpan().StartLinePosition.Line + 1})");

One thought on “Using Roslyn to find all references

Add yours

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at

Up ↑

%d bloggers like this: