如何使用Roslyn确定XML doc评论中的cref类型?

时间:2015-01-07 22:54:29

标签: c# roslyn

我有一个文档评论:

/// <summary>This is an xml doc comment <see cref="MyClass" /></summary>

我如何让Roslyn为我解析这个并给我确定类型的MyClass?

1 个答案:

答案 0 :(得分:1)

您需要找到与类型名称对应的CrefSyntax节点,然后您可以使用SemanticModel.GetSymbolInfo()获取所需的ISymbol

string code = @"namespace Foo
{
    /// <summary>This is an xml doc comment <see cref=""MyClass"" /></summary>
    class MyClass {}
}";

var tree = SyntaxFactory.ParseSyntaxTree(code);

CrefSyntax cref = tree.GetRoot()
    .DescendantNodes(descendIntoTrivia: true)
    .OfType<CrefSyntax>()
    .FirstOrDefault();

var compliation = CSharpCompilation.Create("foo").AddSyntaxTrees(tree);
var model = compliation.GetSemanticModel(tree);

ISymbol symbol = model.GetSymbolInfo(cref).Symbol;