简体   繁体   English

使用 Roslyn 将班级成员添加到特定位置?

[英]Adding class members into specific locations using Roslyn?

I'm using ClassDeclarationSyntax.AddMembers() to add methods to the class.我正在使用ClassDeclarationSyntax.AddMembers()向类添加方法。 The method appears in the class, but I would like to know how to add the method to a specific place.该方法出现在类中,但我想知道如何将方法添加到特定位置。 As of now, they are added to the #if directive at the end of the class.到目前为止,它们被添加到类末尾的#if指令中。

Roslyn version: 4.4.0罗斯林版本:4.4.0

Running the code:运行代码:

var tree = CSharpSyntaxTree.ParseText(@"
namespace Test
{
public class A
{
#if !SILVERLIGHT
public int someField;
#endif
}
}");

var classNode = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();

var previousWhiteSpacesToken =
    SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.None, SyntaxTriviaList.Empty);

var method = SyntaxFactory.MethodDeclaration( //
        SyntaxFactory.PredefinedType( //
            SyntaxFactory.Token(SyntaxKind.IntKeyword)), "CalculateSize") //
    .WithModifiers(SyntaxTokenList.Create(previousWhiteSpacesToken)
        .Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword))) //
    .WithBody(SyntaxFactory.Block()).NormalizeWhitespace();

var newClassNode = classNode.AddMembers(method).NormalizeWhitespace();
Console.WriteLine(newClassNode.ToString());

This result will be obtained:会得到这样的结果:

public class A
{
#if !SILVERLIGHT
    public int someField;
    public int CalculateSize()
    {
    }
#endif
}

I know that there is a way to do this, but this result can only guarantee correctness:我知道有办法做到这一点,但这个结果只能保证正确性:

public class A
{
    public int CalculateSize()
    {
    }
#if !SILVERLIGHT
    public int someField;
#endif
}

I expected this result:我期待这个结果:

public class A
{
#if !SILVERLIGHT
    public int someField;
#endif
    public int CalculateSize()
    {
    }  
}

The problem here is that #endif is the leading trivia of } that closes the class declaration.这里的问题是#endif是关闭类声明的}的主要琐事。 So, when you just add a member, the #endif remains as the leading trivia of } .因此,当您只添加一个成员时, #endif仍然是}的主要琐事。

What you need to do is to move the leading trivia of } to be the leading trivia of the new method you are adding.您需要做的是将}的主要琐事移动为您要添加的新方法的主要琐事。

Here is the modified version of your code:这是您的代码的修改版本:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

var tree = CSharpSyntaxTree.ParseText(@"
namespace Test
{
public class A
{
#if !SILVERLIGHT
public int someField;
#endif
}
}");

var classNode = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();

var previousWhiteSpacesToken =
    SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.None, SyntaxTriviaList.Empty);

var method = SyntaxFactory.MethodDeclaration( //
        SyntaxFactory.PredefinedType( //
            SyntaxFactory.Token(SyntaxKind.IntKeyword)), "CalculateSize") //
    .WithModifiers(SyntaxTokenList.Create(previousWhiteSpacesToken)
        .Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword))) //
    .WithBody(SyntaxFactory.Block())
    .WithLeadingTrivia(classNode.CloseBraceToken.LeadingTrivia).NormalizeWhitespace(); // copy '}' leading trivia to the new method.

// remove leading trivia from '}', they are moved previously to the new method.
var newClassNode = classNode.WithCloseBraceToken(classNode.CloseBraceToken.WithLeadingTrivia()).AddMembers(method).NormalizeWhitespace();
Console.WriteLine(newClassNode.ToString());

This prints the following (exactly your expectation):这将打印以下内容(正是您的期望):

public class A
{
#if !SILVERLIGHT
    public int someField;
#endif
    public int CalculateSize()
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM