24

我希望能够在使用 Roslyn 解析 C# 源代码时阅读 XML 文档注释。

/// <summary>
/// Documentation...
/// </summary>

我尝试在 ParseOptions 中设置 ParseDocumentationComments,但它似乎没有效果?

var parseOptions = ParseOptions.Default.WithParseDocumentationComments(true);
SyntaxTree unit = SyntaxTree.ParseFile(file, parseOptions);
4

3 回答 3

33

您需要:

  1. 查看LeadingTrivia包含 XML 文档注释的语法
  2. 构造一个Compilation,找到Symbol具有 XML 文档注释的那个并使用GetDocumentationComment()它上面的方法。

一个完整的例子:

using Roslyn.Compilers.CSharp;
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var tree = SyntaxTree.ParseText(@"
/// <summary>This is an xml doc comment</summary>
class C
{
}");
        var classNode = (ClassDeclarationSyntax)tree.GetRoot().Members.First();
        var trivia = classNode.GetLeadingTrivia().Single(t => t.Kind == SyntaxKind.DocumentationCommentTrivia);
        var xml = trivia.GetStructure();
        Console.WriteLine(xml);

        var compilation = Compilation.Create("test", syntaxTrees: new[] { tree });
        var classSymbol = compilation.GlobalNamespace.GetTypeMembers("C").Single();
        var docComment = classSymbol.GetDocumentationComment();
        Console.WriteLine(docComment.SummaryTextOpt);
    }
}
于 2013-04-02T15:52:37.423 回答
7

只是为了更新上面的 Kevin Pilch 答案

  1. 通过 nuget 添加 Microsoft.CodeAnalysis.CSharp

  2. 代码如下:

    using System;
    using System.Linq;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    
    class Program
    {
        static void Main(string[] args)
        {
            var tree = CSharpSyntaxTree.ParseText(@"
    /// <summary> This is an xml doc comment </summary>
    class C
    {
    }");
            var root = (CompilationUnitSyntax) tree.GetRoot();
            var classNode = (ClassDeclarationSyntax) (root.Members.First());
    
            var trivias = classNode.GetLeadingTrivia();
            var xmlCommentTrivia = trivias.FirstOrDefault(t => t.Kind() == SyntaxKind.SingleLineDocumentationCommentTrivia);
            var xml = xmlCommentTrivia.GetStructure();
            Console.WriteLine(xml);
    
            var compilation = CSharpCompilation.Create("test", syntaxTrees: new[] {tree});
            var classSymbol = compilation.GlobalNamespace.GetTypeMembers("C").Single();
            var docComment = classSymbol.GetDocumentationCommentXml();
            Console.WriteLine(docComment);
        }
    }
    
于 2017-07-12T14:29:56.800 回答
3

更新 :

FirstOrDefault方法不再可用。所以enumerator改用。

更新代码:

using System;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

class Program
{
    static void Main(string[] args)
    {
        var tree = CSharpSyntaxTree.ParseText(@"
/// <summary> This is an xml doc comment </summary>
class C
{
}");
        var root = (CompilationUnitSyntax) tree.GetRoot();
        var classNode = (ClassDeclarationSyntax) (root.Members.First());

        var trivias = classNode.GetLeadingTrivia();
        var enumerator = trivias.GetEnumerator();
        while (enumerator.MoveNext())
        {
           var trivia = enumerator.Current;
           if(trivia.Kind().Equals(SyntaxKind.SingleLineDocumentationCommentTrivia))
           {
               var xml = trivia.GetStructure();
               Console.WriteLine(xml);
           }
        }
    }
}
于 2017-12-06T13:33:03.413 回答