SyntaxFactory.ParseStatement 生成额外的空格
SyntaxFactory.ParseStatement Generates extra spaces
我想创建一个基于 json 值的方法。
这是我的 json :
"handler": {
"logic": "var response = new IsoMessage(context.Request.Id + 10); \n for (int i = 0; i < 128; i++)\n if ((i != 64) && context.Request.Has(i)) \n response[i] = context.Request[i]; \n\n await Task.Delay(200); \n\n await context.SourceLink.SendAsync(response);"
}
当它被解析,用 whitespace 归一化,并添加到一个方法中时,打印的项目格式不正确。我该如何解决?
这是创建方法语法的代码:
var syntax = SyntaxFactory.ParseStatement(logic).NormalizeWhitespace();
// Create a method public async Task ProcessAsync(IncomingMessageContext context)
var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(typeof(Task).Name), "ProcessAsync")
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
.AddModifiers(SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.WithParameterList(parameterList)
.WithBody(SyntaxFactory.Block(syntax));
打印前是代码:
var code = @namespace.NormalizeWhitespace().ToFullString();
输出是这样的:
public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
{
var response = new IsoMessage(context.Request.Id + 10); for ( int i = 0 ; i < 128 ; i ++ ) if ( ( i != 64 ) && context . Request . Has ( i ) ) response [ i ] = context . Request [ i ] ; await Task . Delay ( 200 ) ; await context . SourceLink . SendAsync ( response ) ;
}
函数中有很多space,没有应用换行符。 :(
正在分析的代码包含 多个 语句,而不是单个语句。如果您注意到第一条语句:
var response = new IsoMessage(context.Request.Id + 10);
格式正确,看起来不像这样:
var response = new IsoMessage ( context . Request . Id + 10 ) ;
因此,在使用 SyntaxFactory.ParseStatement()
时,如果您的代码包含多个语句,则用一对花括号 ({...}
) 将所有语句括起来并转换为 BlockSyntax
,这将随后将代码解析为“块”:
var syntax = (BlockSyntax) SyntaxFactory.ParseStatement("{" + logic + "}");
// ...additional code transformations that adds `ProcessAync()`, etc....
var code = @namespace.NormalizeWhitespace().ToFullString();
同样在方法声明中删除 SyntaxFactory.Block()
因为它现在应该转换为正确的类型:
.WithBody(syntax);
那么你的代码应该输出这个:
public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
{
var response = new IsoMessage(context.Request.Id + 10);
for (int i = 0; i < 128; i++)
if ((i != 64) && context.Request.Has(i))
response[i] = context.Request[i];
await Task.Delay(200);
await context.SourceLink.SendAsync(response);
}
我想创建一个基于 json 值的方法。 这是我的 json :
"handler": {
"logic": "var response = new IsoMessage(context.Request.Id + 10); \n for (int i = 0; i < 128; i++)\n if ((i != 64) && context.Request.Has(i)) \n response[i] = context.Request[i]; \n\n await Task.Delay(200); \n\n await context.SourceLink.SendAsync(response);"
}
当它被解析,用 whitespace 归一化,并添加到一个方法中时,打印的项目格式不正确。我该如何解决?
这是创建方法语法的代码:
var syntax = SyntaxFactory.ParseStatement(logic).NormalizeWhitespace();
// Create a method public async Task ProcessAsync(IncomingMessageContext context)
var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(typeof(Task).Name), "ProcessAsync")
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
.AddModifiers(SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.WithParameterList(parameterList)
.WithBody(SyntaxFactory.Block(syntax));
打印前是代码:
var code = @namespace.NormalizeWhitespace().ToFullString();
输出是这样的:
public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
{
var response = new IsoMessage(context.Request.Id + 10); for ( int i = 0 ; i < 128 ; i ++ ) if ( ( i != 64 ) && context . Request . Has ( i ) ) response [ i ] = context . Request [ i ] ; await Task . Delay ( 200 ) ; await context . SourceLink . SendAsync ( response ) ;
}
函数中有很多space,没有应用换行符。 :(
正在分析的代码包含 多个 语句,而不是单个语句。如果您注意到第一条语句:
var response = new IsoMessage(context.Request.Id + 10);
格式正确,看起来不像这样:
var response = new IsoMessage ( context . Request . Id + 10 ) ;
因此,在使用 SyntaxFactory.ParseStatement()
时,如果您的代码包含多个语句,则用一对花括号 ({...}
) 将所有语句括起来并转换为 BlockSyntax
,这将随后将代码解析为“块”:
var syntax = (BlockSyntax) SyntaxFactory.ParseStatement("{" + logic + "}");
// ...additional code transformations that adds `ProcessAync()`, etc....
var code = @namespace.NormalizeWhitespace().ToFullString();
同样在方法声明中删除 SyntaxFactory.Block()
因为它现在应该转换为正确的类型:
.WithBody(syntax);
那么你的代码应该输出这个:
public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
{
var response = new IsoMessage(context.Request.Id + 10);
for (int i = 0; i < 128; i++)
if ((i != 64) && context.Request.Has(i))
response[i] = context.Request[i];
await Task.Delay(200);
await context.SourceLink.SendAsync(response);
}