首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NRefactory在代码中查找XML注释

NRefactory在代码中查找XML注释
EN

Stack Overflow用户
提问于 2016-02-04 00:52:47
回答 1查看 121关注 0票数 0

我希望获得与使用NRefactory的方法相关联的xml文档。我在这个回答中发现了下面的代码,这让我的脚都湿了

代码语言:javascript
复制
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文章found 这里相关联的这里应用程序。

演示的作者不使用"ToTypeSystem()“方法。相反,他穿过这棵树。以下是他所做工作的一些片段:

代码语言:javascript
复制
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#代码段中的方法相关联的文档。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-04 01:02:54

我确实找到了一个方法,它将为我提供与NRefactory API中的方法相关联的文档。它是具有两个重载的"GetDocumentation“方法。

  • DocumentationComment GetDocumentation(IEntity实体);
  • DocumentationComment GetDocumentation(IUnresolvedEntity entity,IEntity resolvedEntity);

下面是使用的示例

代码语言:javascript
复制
                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);
                    }
                }

顺便说一下,我正在使用NRefactorize的5.0版本。我记得在某个地方读到版本5.0中包含抽象语法树中的注释

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35190623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档