栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Roslyn查找对方法的所有引用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Roslyn查找对方法的所有引用

您可能正在寻找

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;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568524.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号