我有一个文档评论:
/// <summary>This is an xml doc comment <see cref="MyClass" /></summary>
我如何让Roslyn为我解析这个并给我确定类型的MyClass?
答案 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;