Roslyn - 我需要找到方法参考,我有以下代码。 但问题是当我有两个同名的方法时。
E.g。
方法1:
public string GetName(string id)
{
}
public string GetName()
{
}
这里我有两个同名的方法" GetName"我的代码是找到第一个方法(FirstOrDefault也是一个原因)但有没有办法找到方法参考和参数列表?
代码:
public static async Task<List<ReferenceLocation>> FindMethodReferences(string solutionPath, string methodName)//*Here methodName is "GetName"*
{
var msWorkspace = MSBuildWorkspace.Create();
ISymbol methodSymbol = null;
bool found = false;
var solution = await msWorkspace.OpenSolutionAsync(solutionPath);
foreach (var project in solution.Projects)
{
foreach (var document in project.Documents)
{
var model = await document.GetSemanticModelAsync();
var methodInvocation = await document.GetSyntaxRootAsync();
InvocationExpressionSyntax node = null;
try
{
***//Is there any condition I should add here to find the method references with arg list***
node = methodInvocation.DescendantNodes()
.OfType<InvocationExpressionSyntax>().FirstOrDefault(x => ((MemberAccessExpressionSyntax)x.Expression).Name.ToString() == methodName);
if (node == null)
continue;
}
catch
{
continue;
}
methodSymbol = model.GetSymbolInfo(node).Symbol;
found = true;
break;
}
if (found) break;
}
return SymbolFinder.FindReferencesAsync(methodSymbol, solution).Result.SelectMany(item => item.Locations).ToList();
}