我希望扫描一组.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? */
}发布于 2015-08-07 04:00:15
您可能正在寻找SymbolFinder类,特别是FindAllReferences方法。
听起来你在熟悉Roslyn方面有些困难。我有一系列的博客帖子来帮助人们认识罗斯林,叫做Learn Roslyn Now。
正如@SLaks提到的,您将需要访问我在Part 7: Introduction to the Semantic Model中介绍的语义模型
下面是一个示例,向您展示如何使用该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;发布于 2019-03-12 06:49:54
一本关于问题和最终修复的日志,我让罗斯林与VS2017合作:
当MSBuildWorkspace WorkspaceFailed事件连接到时,空VS2017项目的原因将变得可见。
第一轮失败是:
MSB0001:内部MSBuild错误: Microsoft.Build.Utilities.ToolLocationHelper的类型信息在白名单缓存中显示为Microsoft.Build.Utilities.ToolLocationHelper、Microsoft.Build.Utilities.Core、Version=15.1.0.0、Culture=neutral、PublicKeyToken=b03f5f7f11d50a3a,但无法加载该类型。意外为空])
已通过安装NuGet packages Microsoft.Build.Locator 1.1.2和Microsoft.Build.Utilities.Core 15.9.20修复此问题
第二轮失败是:
Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets:在处理文件“”C:\Users...vbproj“”时失败,消息为: C:\Program Files (x86)\Microsoft Visual Microsoft.Build.Tasks.Core (1491,5):无法从程序集Microsoft.Build.Tasks.Core、Version=15.1.0.0、Culture=neutral、PublicKeyToken=b03f5f7f11d50a3a加载Msbuild任务。“”未能加载文件或程序集“Microsoft.Build.Tasks.Core,Version=15.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。系统找不到指定的文件。确认声明正确,程序集及其所有依赖项都可用,并且任务包含实现Microsoft.Build.Framework.ITask的公共类。])
已通过添加NuGet Microsoft.Build.Tasks.Core 15.9.20修复此问题
第三轮失败是:Microsoft在处理文件'C:\Users...vbproj‘时失败,并显示消息: C:\Program Files (x86)\Microsoft Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". (1657,5):无法从程序集"C:\Program Files (x86)\Microsoft“实例化"GetReferenceNearestTargetFrameworkTask”任务请验证任务程序集是使用与计算机上安装的Microsoft.Build.Framework程序集版本相同的版本生成的,并且您的主机应用程序没有缺少Microsoft.Build.Framework的绑定重定向。无法将'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask‘类型的对象强制转换为“”Microsoft.Build.Framework.ITask“”类型。])
注意,项目的Microsoft.Build.Framework.dll = 15.1.0.0,但是消息提到“MSBuild15.0\Bin”
添加到app.config -修复了它!cf enter link description here I现在可以从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 -->https://stackoverflow.com/questions/31861762
复制相似问题