Getting method arguments with Roslyn
- by Kevin Burton
I can get a list from the solution of all calls to a particuliar method using the following code:
var createCommandList = new List<MethodSymbol>();
INamedTypeSymbol interfaceSymbol = (from p in solution.Projects select p.GetCompilation().GetTypeByMetadataName("BuySeasons.BsiServices.DataResource.IBsiDataConnection")).FirstOrDefault();
foreach (ISymbol symbol in interfaceSymbol.GetMembers("CreateCommand"))
{
if (symbol.Kind == CommonSymbolKind.Method &&
symbol is MethodSymbol)
{
createCommandList.Add(symbol as MethodSymbol);
}
}
foreach (MethodSymbol methodSymbol in createCommandList)
{
foreach (ReferencedSymbol referenceSymbol in methodSymbol.FindReferences(solution))
{
foreach (ReferenceLocation referenceLocation in from l in referenceSymbol.Locations orderby l.Document.FilePath select l)
{
if (referenceLocation.Location.GetLineSpan(false).StartLinePosition.Line ==
referenceLocation.Location.GetLineSpan(false).EndLinePosition.Line)
{
Debug.WriteLine(string.Format("{0} {1} at {2} {3}/{4} - {5}", methodSymbol.Name,
"(" + string.Join(",", (from p in methodSymbol.Parameters
select p.Type.Name + " " + p.Name).ToArray()) + ")",
Path.GetFileName(referenceLocation.Location.GetLineSpan(false).Path),
referenceLocation.Location.GetLineSpan(false).StartLinePosition.Line,
referenceLocation.Location.GetLineSpan(false).StartLinePosition.Character,
referenceLocation.Location.GetLineSpan(false).EndLinePosition.Character));
}
else
{
throw new ApplicationException("Call spans multiple lines");
}
}
}
}
But this gives me a list of ReferencedSymbol's. Although this gives me the file and line number that the method is called from I would also like to get the specific arguments that the method is called with. How can I either convert what I have or get the same information with Roslyn? (notice the I first load the solution with the Solution.Load method and then loop through to find out where the method is defined/declared (createCommandList)). Thank you.