如何从MethodDeclarationSyntax对象获取程序集名称

时间:2019-03-04 14:55:23

标签: c# roslyn

使用罗斯林(Roslyn),我正在获得如下公共方法:

  var semanticModel = file.GetSemanticModelAsync().Result;
  var classParser = new ClassParser(semanticModel);
  var tree = file.GetSyntaxTreeAsync().Result;
  var methodDeclarations = tree.GetRoot().DescendantNodes()
                            .OfType<MethodDeclarationSyntax>()
                            .Where(method => method.Modifiers.Any(modifier => modifier.Kind() == SyntaxKind.PublicKeyword)).ToList();

如何从MethodDeclarationSyntax对象获取程序集名称?

1 个答案:

答案 0 :(得分:1)

由于已经有了语义模型,因此可以在其中查询符号。这些符号引用了包含在其中的有关装配的信息:

foreach (MethodDeclarationSyntax method in methodDeclarations)
{
    var symbol = semanticModel.GetEnclosingSymbol(method.SpanStart);
    var assembly = symbol.ContainingAssembly;
    var assemblyName = assembly.Identity.Name;
}