c# - 将 C# 代码与 roslyn : comment disappears 合并

标签 c# roslyn

场景:我正在尝试使用 Roslyn 将一组 C# 源代码片段合并为一个代码片段。
问题:解析带有前导注释的不同类时,第一个类(示例中的 SomeClass)上方的注释不会被保留。对于第二个类( AnotherClass ),注释被保留...
下面的代码演示了这个问题。在控制台程序中使用它(并安装 Microsoft.CodeAnalysis.CSharp nuget 包)

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Diagnostics;
using System;

namespace roslyn01
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = new[]
            {
               "// some comment\r\nclass SomeClass {}",
               "// another comment\r\nclass AnotherClass {}",
            };

            // parse all input and collect members
            var members = new List<MemberDeclarationSyntax>();
            foreach (var item in input)
            {
                var syntaxTree = CSharpSyntaxTree.ParseText(item);
                var compilationUnit = (CompilationUnitSyntax)syntaxTree.GetRoot();
                members.AddRange(compilationUnit.Members);
            }

            // assemble all members in a new compilation unit
            var result = SyntaxFactory.CompilationUnit()
                .AddMembers(members.ToArray())
                .NormalizeWhitespace()
                .ToString();

            var expected = @"// some comment
class SomeClass
{
}

// another comment
class AnotherClass
{
}";

            Console.WriteLine(result);
            // the assert fails; the first comment ('// some comment') is missing from the output
            Debug.Assert(expected == result);
        }
    }
}
我错过了什么?

最佳答案

您正在使用 ToString()而不是 ToFullString()变形时CompilationUnitSyntaxstring . ToString()返回一个删除前导和尾随琐事(包括评论)的字符串,但 ToFullString()不会删除这个琐事。您可以在代码文档中看到它:

//
// Description:
//     Returns the string representation of this node, not including its leading and
//     trailing trivia.
//
// Returns:
//     The string representation of this node, not including its leading and trailing
//     trivia.
//
// Comments:
//     The length of the returned string is always the same as Span.Length
public override string ToString();

//
// Description:
//     Returns full string representation of this node including its leading and trailing
//     trivia.
//
// Returns:
//     The full string representation of this node including its leading and trailing
//     trivia.
//
// Comments:
//     The length of the returned string is always the same as FullSpan.Length
public virtual string ToFullString();

关于c# - 将 C# 代码与 roslyn : comment disappears 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63576538/

相关文章:

c# - Json.NET 自定义 JsonConverter 被忽略

c# - 使用 VS 2015 代码分析器出错

c# - 如何在 Visual Studio Code 中向灯泡图标添加自定义 "Implement Interface"选项?

C# 有没有办法列出时间范围?可配置

c# - 表单应该包含对象,还是对象包含表单?

c# - 如何根据参数的动态数量过滤查询

c# - C#.Net 中的文件复制

c++ - Roslyn 是否支持 C++ 生成语法树

c# - 为什么 SyntaxNode.ReplaceNode 会更改 SyntaxTree 选项?

c# - Roslyn 是编译时表达式检查的正确工具吗?