2016-02-04 53 views
0

我想獲得與使用NRefactory的方法關聯的xml文檔。我得到了我的腳溼使用下面的代碼,我在這個answerNRefactory在代碼中查找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應用程序發現的。here

該演示的作者未使用「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#代碼片段的方法的文檔中的方法。

回答

0

我確實找到了一種方法,可以讓我得到與NRefactory API中的方法相關的文檔。它是具有兩個過載的「GetDocumentation」方法

  • DocumentationComment GetDocumentation(IEntity entity);
  • DocumentationComment GetDocumentation(IUnresolvedEntity entity,IEntity 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); 
        } 
       } 

我使用的是5.0版本NRefactorize的的方式的一個例子。我記得在某處讀到5.0版本在抽象語法樹中包含註釋