C# 在代码中查找XML文档注释

C# 在代码中查找XML文档注释,c#,nrefactory,C#,Nrefactory,我想获得与使用NRefactory的方法相关联的xml文档。通过使用我在本文中找到的以下代码,我的脚湿透了 但是,我查看了IUnresolvedTypeDefinition接口,它没有任何“注释”属性。此外,IUnresolvedMethod接口没有任何“注释”属性。我知道可以检索注释,因为我是通过使用与找到的CodeProject文章关联的WinForms应用程序来检索注释的 演示的作者没有使用“ToTypeSystem()”方法。相反,他穿过了这棵树。以下是他所做工作的一些片段: Synta

我想获得与使用NRefactory的方法相关联的xml文档。通过使用我在本文中找到的以下代码,我的脚湿透了

但是,我查看了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代码段中的方法相关联的文档。

我确实找到了一种方法,可以获取与NRefactory API中的方法相关联的文档。“GetDocumentation”方法有两个重载

  • 文档注释获取文档(实体实体)
  • 文档注释获取文档(IUnresolvedEntity、entity resolvedEntity、entity resolvedEntity)
下面是一个用法示例

                SyntaxTree tree = parser.Parse(line, "demo.cs");

                var testClass = tree.Descendants.OfType<TypeDeclaration>().Single(x => x.Members == Method);
                var testClassAttributes = testClass.Attributes.SelectMany(x => x.Attributes).ToArray();

                List<Dictionary<string, object>> myList = new List<Dictionary<string, object>>();

                string nombreControlador = null;
                string rutaControlador = null;
                string actionKeyPath = null;
                string fullControllerPath = null;
                int counter = 0;

                CSharpUnresolvedFile file = tree.ToTypeSystem();



                foreach (IUnresolvedTypeDefinition type in file.TopLevelTypeDefinitions)
                {
                    nombreControlador = type.Name;
                    actionKeyPath = type.Fields.Skip(1).FirstOrDefault().ConstantValue.ToString();
                    fullControllerPath = type.Fields.First().ConstantValue.ToString();
                    rutaControlador = type.FullName;
                    foreach (IUnresolvedMethod method in type.Methods)
                    {
                        string documentation = file.GetDocumentation(method).Trim();

                        XDocument doc = XDocument.Parse("<documentation>" + documentation + "</documentation>");        
                        Dictionary<string, object> myDic = new Dictionary<string, object>();
                        Console.WriteLine(method.Name);
                        myDic.Add("MethodSignature", method.Name);
                        myDic.Add("MethodDescription", doc.Descendants().Select(e => (string)e.Element("summary")).FirstOrDefault());
                        myDic.Add("ActionKeyPath", actionKeyPath == null? "" : actionKeyPath);
                        myDic.Add("Counter", ++counter);
                        myDic.Add("FullControllerPath", fullControllerPath == null? "" : fullControllerPath);
                        myDic.Add("Route", method.Attributes == null ? "" : method.Attributes.Count <= 1 || method.Attributes.Skip(1) == null? "" : method.Attributes.SelectMany(a => a.);
                        myDic.Add("Verb", "");
                        myDic.Add("Input", "");
                        myDic.Add("Output", "");
                        myList.Add(myDic);
                    }
                }
SyntaxTree=parser.Parse(第行,“demo.cs”);
var testClass=tree.subjects.OfType().Single(x=>x.Members==Method);
var testClassAttributes=testClass.Attributes.SelectMany(x=>x.Attributes.ToArray();
List myList=新列表();
字符串nombreControlador=null;
字符串rutaControlador=null;
字符串actionKeyPath=null;
字符串fullControllerPath=null;
int计数器=0;
csharpunsolvedfile=tree.ToTypeSystem();
foreach(文件.TopLevelTypeDefinitions中的IUnresolvedTypeDefinition类型)
{
nombreControlador=type.Name;
actionKeyPath=type.Fields.Skip(1).FirstOrDefault().ConstantValue.ToString();
fullControllerPath=type.Fields.First().ConstantValue.ToString();
rutaControlador=type.FullName;
foreach(类型方法中的IUnresolvedMethod方法)
{
string documentation=file.GetDocumentation(method.Trim();
XDocument doc=XDocument.Parse(“+documentation+”);
Dictionary myDic=新字典();
Console.WriteLine(方法名);
myDic.Add(“MethodSignature”,method.Name);
myDic.Add(“MethodDescription”,doc.subjects()).Select(e=>(string)e.Element(“summary”).FirstOrDefault();
添加(“ActionKeyPath”,ActionKeyPath==null?”:ActionKeyPath);
添加(“计数器”++计数器);
myDic.Add(“FullControllerPath”,FullControllerPath==null?”:FullControllerPath);
myDic.Add(“Route”,method.Attributes==null?”:method.Attributes.Count a.);
myDic.Add(“动词”,“动词”);
myDic.Add(“输入”、“输入”);
myDic.Add(“输出”和“);
myList.Add(myDic);
}
}
顺便说一下,我正在使用5.0版本的NRefactorize。我记得在某个地方读到5.0版在抽象语法树中包含注释

                SyntaxTree tree = parser.Parse(line, "demo.cs");

                var testClass = tree.Descendants.OfType<TypeDeclaration>().Single(x => x.Members == Method);
                var testClassAttributes = testClass.Attributes.SelectMany(x => x.Attributes).ToArray();

                List<Dictionary<string, object>> myList = new List<Dictionary<string, object>>();

                string nombreControlador = null;
                string rutaControlador = null;
                string actionKeyPath = null;
                string fullControllerPath = null;
                int counter = 0;

                CSharpUnresolvedFile file = tree.ToTypeSystem();



                foreach (IUnresolvedTypeDefinition type in file.TopLevelTypeDefinitions)
                {
                    nombreControlador = type.Name;
                    actionKeyPath = type.Fields.Skip(1).FirstOrDefault().ConstantValue.ToString();
                    fullControllerPath = type.Fields.First().ConstantValue.ToString();
                    rutaControlador = type.FullName;
                    foreach (IUnresolvedMethod method in type.Methods)
                    {
                        string documentation = file.GetDocumentation(method).Trim();

                        XDocument doc = XDocument.Parse("<documentation>" + documentation + "</documentation>");        
                        Dictionary<string, object> myDic = new Dictionary<string, object>();
                        Console.WriteLine(method.Name);
                        myDic.Add("MethodSignature", method.Name);
                        myDic.Add("MethodDescription", doc.Descendants().Select(e => (string)e.Element("summary")).FirstOrDefault());
                        myDic.Add("ActionKeyPath", actionKeyPath == null? "" : actionKeyPath);
                        myDic.Add("Counter", ++counter);
                        myDic.Add("FullControllerPath", fullControllerPath == null? "" : fullControllerPath);
                        myDic.Add("Route", method.Attributes == null ? "" : method.Attributes.Count <= 1 || method.Attributes.Skip(1) == null? "" : method.Attributes.SelectMany(a => a.);
                        myDic.Add("Verb", "");
                        myDic.Add("Input", "");
                        myDic.Add("Output", "");
                        myList.Add(myDic);
                    }
                }