如何使用roslyn创建数字横向令牌?

时间:2016-03-26 07:51:26

标签: c# roslyn

我想像下面一样更改InitializerExpressionSyntax节点但发生错误:使用Roslyn.Compilers.CSharp.Syntax.Literal创建数字横向令牌。参数名称: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;
}

1 个答案:

答案 0 :(得分:2)

尝试在SyntaxFactory.Token中创建令牌时; SyntaxFactory.Token函数不支持使用NumbericLiteralToken的SyntaxKind。

相反,您需要按照API的要求使用SyntaxFactory.Literal。

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

源; Rosalyn参考资料

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

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