场景:我正在尝试使用 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
您正在使用ToString()而不是ToFullString()转换CompilationUnitSyntax为string. 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
169 次 |
| 最近记录: |