我想获取与使用 NRefactory 的方法相关联的 xml 文档。我使用在此答案中找到的以下代码弄湿了我的脚
var parser = new CSharpParser();
SyntaxTree tree = parser.Parse(code, "test.cs");
CSharpUnresolvedFile file = tree.ToTypeSystem();
foreach (IUnresolvedTypeDefinition type in file.TopLevelTypeDefinitions) {
foreach (IUnresolvedMethod method in type.Methods) {
Console.WriteLine(method.Name);
}
}
但是,我正在查看 IUnresolvedTypeDefinition 接口,它没有任何“评论”属性。此外,IUnresolvedMethod 接口没有任何“评论”属性。我确实知道可以检索评论,因为我是通过使用与此处找到的 CodeProject 文章相关联的 WinForms 应用程序来检索评论的
演示的作者没有使用“ToTypeSystem()”方法。相反,他遍历树。以下是他所做的一些片段:
SyntaxTree tree = parser.Parse(line, "demo.cs");
foreach (var element in tree.Children)
{
MakeTreeNode(element);
}
static void MakeTreeNode(AstNode node)
{
Console.WriteLine(GetNodeTitle(node));
foreach (AstNode child in node.Children)
{
MakeTreeNode(child);
}
}
static string GetNodeTitle(AstNode node)
{
StringBuilder b = new StringBuilder();
b.Append(node.Role.ToString());
b.Append(": ");
b.Append(node.GetType().Name);
bool hasProperties = false;
foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (p.Name == "NodeType" || p.Name == "IsNull" || p.Name == "IsFrozen" || p.Name == "HasChildren")
continue;
if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum || p.PropertyType == typeof(bool))
{
if (!hasProperties)
{
hasProperties = true;
b.Append(" (");
}
else
{
b.Append(", ");
}
b.Append(p.Name);
b.Append(" = ");
try
{
object val = p.GetValue(node, null);
b.Append(val != null ? val.ToString() : "**null**");
}
catch (TargetInvocationException ex)
{
b.Append("**" + ex.InnerException.GetType().Name + "**");
}
}
}
if (hasProperties)
b.Append(")");
return b.ToString() + "\n";
}
我想知道 NRefactory API 中是否有一种方法可以获取与 C# 代码中的方法相关联的文档。