找到所有与罗斯林有关的方法

本文关键字:方法 罗斯林 | 更新日期: 2023-09-27 18:06:54

我希望扫描一组.cs文件,看看哪些文件调用Nullable<T>Value属性(找到所有引用)。例如,这将匹配:

class Program
{
    static void Main()
    {
        int? nullable = 123;
        int value = nullable.Value;
    }
}

我发现了Roslyn并查看了一些示例,但其中许多都过时了,API非常庞大。我该怎么做呢?

我在解析语法树后卡住了。这是我目前所看到的:

public static void Analyze(string sourceCode)
{
    var tree = CSharpSyntaxTree.ParseText(sourceCode);
    tree./* ??? What goes here? */
}

找到所有与罗斯林有关的方法

您可能正在寻找SymbolFinder类,特别是FindAllReferences方法。

听起来你好像很难熟悉Roslyn。我有一系列博客文章来帮助人们认识罗斯林,叫做"现在学习罗斯林"。

正如@SLaks提到的,您将需要访问我在第7部分:语义模型介绍

中介绍的语义模型。

下面的示例向您展示了如何使用该API。如果可以的话,我会使用MSBuildWorkspace并从磁盘加载项目,而不是像这样在AdHocWorkspace中创建项目。

var mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var ws = new AdhocWorkspace();
//Create new solution
var solId = SolutionId.CreateNewId();
var solutionInfo = SolutionInfo.Create(solId, VersionStamp.Create());
//Create new project
var project = ws.AddProject("Sample", "C#");
project = project.AddMetadataReference(mscorlib);
//Add project to workspace
ws.TryApplyChanges(project.Solution);
string text = @"
class C
{
    void M()
    {
        M();
        M();
    }
}";
var sourceText = SourceText.From(text);
//Create new document
var doc = ws.AddDocument(project.Id, "NewDoc", sourceText);
//Get the semantic model
var 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;

一个关于问题和最终修复的日志,我让Roslyn使用VS2017:

当MSBuildWorkspace WorkspaceFailed事件被钩入时,VS2017项目空的原因变得可见。

第一轮失败是:

MSB0001:内部MSBuild错误:Microsoft.Build.Utilities. toollocationhelper的类型信息在白名单缓存中存在为Microsoft.Build.Utilities。预发布,Microsoft.Build.Utilities。Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,但类型无法加载。意外null])

通过安装NuGet软件包Microsoft.Build.Locator 1.1.2 &Microsoft.Build.Utilities.Core 15.9.20

第二轮失败是:

Msbuild在处理文件"C:'Users…"时失败Vbproj '与消息:C:'Program Files (x86)'Microsoft Visual Studio'2017'Enterprise'MSBuild'15.0'Bin' microt.common . currentversion。目标:(1491,5):"Microsoft.Build.Tasks.AssignProjectConfiguration"任务无法从程序集加载。Microsoft.Build.Tasks。Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a。无法加载文件或程序集" Microsoft.Build.Tasks "。Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。确认声明是正确的,程序集及其所有依赖项是可用的,并且任务包含实现Microsoft.Build.Framework.ITask的公共类。[/p>

添加NuGet Microsoft.Build.Tasks.Core 15.9.20

第三轮失败为:=== Msbuild处理文件"C:'Users…"时失败。Vbproj '与消息:C:'Program Files (x86)'Microsoft Visual Studio'2017'Enterprise'MSBuild'15.0'Bin' microt.common . currentversion。目标:(1657,5):"GetReferenceNearestTargetFrameworkTask"无法从程序集实例化任务C:'Program Files (x86)'Microsoft Visual Studio'2017'Enterprise'Common7'IDE'CommonExtensions'Microsoft'NuGet'NuGet. build . tasks .dll"请验证已使用与计算机上安装的Microsoft.Build.Framework程序集相同的版本构建任务程序集并且您的主机应用程序没有丢失Microsoft.Build.Framework的绑定重定向。无法强制转换"NuGet.Build.Tasks"类型的对象。gettreferencenearesttargetframeworktask '来输入'Microsoft.Build.Framework.ITask'.])

请注意,项目的Microsoft.Build.Framework.dll = 15.1.0.0,但消息中提到"MSBuild'15.0'Bin"

添加到app.config -修复它!我现在可以从VS2017解决方案中加载项目

  <!-- vvv Roslyn manual fixup https://github.com/Microsoft/msbuild/issues/2369 -->
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
  <!-- ^^^ Roslyn manual fixup https://github.com/Microsoft/msbuild/issues/2369 -->