문제

I want to create new Method using Roslyn CTP. I create a new instance type of MethodDeclarationSyntax.

 MethodDeclarationSyntax lMethodDeclarationSyntax = Syntax.MethodDeclaration(
            Syntax.List<AttributeListSyntax>(),
            Syntax.TokenList(
                Syntax.Token(SyntaxKind.PublicKeyword)),
            Syntax.IdentifierName("MemoryStream"),
            null,
            Syntax.Identifier("Serialize"),
            null,
            Syntax.ParameterList(),
            Syntax.List<TypeParameterConstraintClauseSyntax>(),
            Syntax.Block(lList));

But in response i get something like this:

publicMemoryStreamSerialize(){MemoryStream lMemoryStream = new MemoryStream();StreamWriter lStreamWriter = new StreamWriter(lMemoryStream);lStreamWriter.Write(IntVariable);lStreamWriter.Write(ExVariable.ToStream());return lMemoryStream;}  

What i missed?

도움이 되었습니까?

해결책

Add NormalizeWhitespace at the end to add normalized whitespace trivia into your tree.

MethodDeclarationSyntax lMethodDeclarationSyntax =
    Syntax.MethodDeclaration(
        Syntax.List<AttributeListSyntax>(),
        Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword)),
        Syntax.IdentifierName("MemoryStream"),
        null,
        Syntax.Identifier("Serialize"),
        null,
        Syntax.ParameterList(),
        Syntax.List<TypeParameterConstraintClauseSyntax>(),
        Syntax.Block(lList))
    .NormalizeWhitespace();

You can do this to any syntax node and it will return a node with all the whitespace trivia normalized throughout the tree. Remember that node trees are immutable so it will not have any effect on the already existing tree but will create a new one.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top