简体   繁体   中英

How create Numeric literal token with roslyn?

I want to change InitializerExpressionSyntax node like below but an error occurred :Use Roslyn.Compilers.CSharp.Syntax.Literal to create numeric literal token. Parameter name :kind

public override syntaxnode VisitInitializerExpression (InitializerExpressionSyntax node )
{
    SeparatedSyntaxList<ExpressionSyntax> list =new SeparatedSyntaxList<ExpressionSyntax> ();
    foreach  (var item in RandomByteArray)
    {
        SyntaxToken t=SyntaxFactory.Token (default (SyntaxTriviaList),SyntaxKind.NumericLiteralToken, item.ToString (),item. ToString  (),(default (SyntaxTriviaList));//error is here
        list.Add (SyntaxFactory.LiteralExpression  (SyntaxKind.NumericLiteralExpression,t));
   }
    node=node. WithExpressions(list);
    return node;
}

When trying to create a token, within SyntaxFactory.Token; that is a SyntaxKind of a NumbericLiteralToken is not supported by the SyntaxFactory.Token function.

Instead, you'll need to use SyntaxFactory.Literal as requested by the API.

SyntaxFactory.Literal(default(SyntaxTriviaList), item.ToString(), item.ToString(), item.ToString(), default(SyntaxTriviaList));

Source; Rosalyn Reference Source

(SyntaxFactory.Token: http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Syntax/SyntaxFactory.cs,241 )

(SyntaxFactory.Literal: http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Syntax/SyntaxFactory.cs,585 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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