您可能正在寻找
SymbolFinder类,尤其是
FindAllReferences方法。
听起来您在熟悉罗斯林时遇到了一些麻烦。我提供了一系列博客文章,以帮助人们了解Roslyn,称为Learn Roslyn Now。
正如@SLaks提到的那样,您将需要访问我在第7部分:语义模型简介中介绍的语义模型。
这是一个示例,向您展示如何使用API。如果可以的话,我将使用
MSBuildWorkspace磁盘中的项目并将其加载,而不是
AdHocWorkspace像这样创建它。
var mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);var ws = new AdhocWorkspace();//Create new solutionvar solId = SolutionId.CreateNewId();var solutionInfo = SolutionInfo.Create(solId, VersionStamp.Create());//Create new projectvar project = ws.AddProject("Sample", "C#");project = project.AddmetadataReference(mscorlib);//Add project to workspacews.TryApplyChanges(project.Solution);string text = @"class C{ void M() { M(); M(); }}";var sourceText = SourceText.From(text);//Create new documentvar doc = ws.Adddocument(project.Id, "NewDoc", sourceText);//Get the semantic modelvar model = doc.GetSemanticModelAsync().Result;//Get the syntax node for the first invocation to M()var methodInvocation = doc.GetSyntaxRootAsync().Result.DescendantNodes().OfType<InvocationexpressionSyntax>().First();var methodSymbol = model.GetSymbolInfo(methodInvocation).Symbol;//Finds all references to M()var referencesToM = SymbolFinder.FindReferencesAsync(methodSymbol, doc.Project.Solution).Result;


