如何使用 Roslyn 确定 XML 文档注释中 cref 的类型?
How can I determine the type of a cref in an XML doc comment with Roslyn?
我有这样的文档评论:
/// <summary>This is an xml doc comment <see cref="MyClass" /></summary>
如何让 Roslyn 为我解析它并给我确定的 MyClass 类型?
你需要找到类型名称对应的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;
我有这样的文档评论:
/// <summary>This is an xml doc comment <see cref="MyClass" /></summary>
如何让 Roslyn 为我解析它并给我确定的 MyClass 类型?
你需要找到类型名称对应的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;