我正在用C#编写一个程序,用于从代码中提取注释。我正在使用Roslyn编译器来做到这一点。太好了,因为我只是访问整个抽象语法树,并从解决方案中的文件中获取SingleLineComment琐事,MultiLineComment琐事和DocumentationComment琐事语法。但是存在一个问题,因为程序员经常这样写注释:
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
Run Code Online (Sandbox Code Playgroud)
您可以看到它们是三个单行注释,但我希望它们可以从代码中作为一个注释获取。我可以通过罗斯林实现这一目标,还是有另一种方式?因为这是程序员使用单行注释语法编写多行注释时经常出现的情况。
我提取注释的代码如下所示:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
namespace RoslynPlay
{
public class CommentStore
{
public List<Comment> Comments { get; } = new List<Comment>();
public void AddCommentTrivia(SyntaxTrivia trivia,
LocationStore commentLocationstore, string fileName)
{
if (trivia.Kind() == SyntaxKind.SingleLineCommentTrivia)
{
Comments.Add(new SingleLineComment(trivia.ToString(),
trivia.GetLocation().GetLineSpan().EndLinePosition.Line + 1, commentLocationstore)
{
FileName = fileName,
}); …Run Code Online (Sandbox Code Playgroud)