gpt4 book ai didi

c# - SymbolFinder.FindReferencesAsync 未找到任何内容

转载 作者:行者123 更新时间:2023-12-02 17:41:00 26 4
gpt4 key购买 nike

我想查找解决方案中的所有 PropertyChangedEventHandler 事件,然后查找添加到这些事件的所有监听器。但我似乎无法获得事件列表。

这是正在分析的解决方案中的所有代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoslynTarget
{
public class Example : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };

public void DoNothing() { }
}
}

这是我分析它的代码。 references.Count == 1r.Locations.Count == 0,但应该找到一个位置。这是怎么回事?

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.MSBuild;

namespace RoslynTest
{
class Program
{
static void Main(string[] args)
{
const string solutionPath = @"C:\Users\<user>\Code\RoslynTarget\RoslynTarget.sln";
const string projectName = "RoslynTarget";

var msWorkspace = MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } });
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;

var project =
solution.Projects
.Where(proj => proj.Name == projectName)
.First();

var compilation = project.GetCompilationAsync().Result;
var eventType = compilation.ResolveType("System.ComponentModel.PropertyChangedEventHandler").First();
var references = SymbolFinder.FindReferencesAsync(eventType, solution).Result;

foreach (var r in references)
{
foreach (var loc in r.Locations)
{
// ...
}
}
}
}
}

扩展.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;

namespace RoslynTest
{
public static class Extensions
{
public static IEnumerable<INamedTypeSymbol> ResolveType(this Compilation compilation, string classFullName)
{
return new IAssemblySymbol[] { compilation.Assembly }
.Concat(compilation.References
.Select(compilation.GetAssemblyOrModuleSymbol)
.OfType<IAssemblySymbol>())
.Select(asm => asm.GetTypeByMetadataName(classFullName))
.Where(cls => cls != null);
}
}
}

最佳答案

最近我做了类似的事情,试图在完整的解决方案中找到方法的引用。要使用 FindReferenceAsync,您必须首先创建符号模型并从那里找到符号。获得该符号后,您可以使用 FindReferenceAsync

这是我使用的代码片段,它正在工作:

var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
foreach (var project in solution.Projects)
{
foreach (var document in project.Documents)
{
var model = document.GetSemanticModelAsync().Result;

var methodInvocation = document.GetSyntaxRootAsync().Result;
InvocationExpressionSyntax node = null;
try
{
node = methodInvocation.DescendantNodes().OfType<InvocationExpressionSyntax>()
.Where(x => ((MemberAccessExpressionSyntax)x.Expression).Name.ToString() == methodName).FirstOrDefault();

if (node == null)
continue;
}
catch(Exception exception)
{
// Swallow the exception of type cast.
// Could be avoided by a better filtering on above linq.
continue;
}

methodSymbol = model.GetSymbolInfo(node).Symbol;
found = true;
break;
}

if (found) break;
}

foreach (var item in SymbolFinder.FindReferencesAsync(methodSymbol, solution).Result)
{
foreach (var location in item.Locations)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Project Assembly -> {0}", location.Document.Project.AssemblyName);
Console.ResetColor();
}

}

这是complete working code 。如果您想可视化 Roslyn 树,那么您可以尝试使用 Roslyn tree visualizer查看代码结构。

更新

根据 OP 安装评论中的讨论 Roslyn SDK解决了这个问题。假设使用 GetCompilationAsync 时,与 Nuget dll 相比,SDK 中可能有一些更新来生成符号信息。

关于c# - SymbolFinder.FindReferencesAsync 未找到任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com