我正在尝试使用 Roslyn 来识别方法组引用背后的符号。这SemanticModel.GetSymbolInfo
在早期版本的 Roslyn 中运行良好,但在最新版本中找不到符号。执行此映射的正确方法是什么?
var ws = new AdhocWorkspace();
var proj = ws.AddProject("test", "C#")
.AddMetadataReference(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
proj = proj.WithParseOptions(proj.ParseOptions.WithFeatures(new Dictionary<string, string> { { "IOperation", "true" }}));
var doc = proj.AddDocument("test.cs", SourceText.From(@"namespace Test {
public class Program
{
public static void Main()
{
Func<int> x = Foo; // want to map Foo to one of the methods below!
}
private static int Foo() => 7;
private static int Foo(int x) => 8
}
}"));
proj = doc.Project;
var compilation = proj.GetCompilationAsync().Result;
var tree = doc.GetSyntaxTreeAsync().Result;
var model = compilation.GetSemanticModel(tree);
var fooToken = tree.GetRoot().DescendantTokens()
.First(t => t.Text.ToString() == "Foo");
Console.WriteLine(model.GetSymbolInfo(fooToken.Parent).Symbol); // null
Console.WriteLine(model.GetMemberGroup(fooToken.Parent).Length); // 2
作为示例,显示 GetMemberGroup 确实返回结果,但它返回两种 Foo() 方法的结果,而不是我引用的特定方法。
如何提取确切的引用符号?