方法体的BlockSyntax中的格式错误

时间:2017-06-05 20:18:51

标签: c# roslyn

我编写的代码为WebAPI控制器方法调用生成了一个类。当我尝试生成方法体时,我遇到了像这样的字符串格式问题:

    public async Task<String> HttpPost(Boolean isSuccess)
    {
            var taskCompletion = new TaskCompletionSource<IRestResponse>(); var  request  =  RestSharpExtensions . PostRequest ( "Test" ,  "HttpPost" ,  isSuccess . ToString ( ) ) ;  var  handle  =  Client . ExecuteAsync ( request ,  r  =>  taskCompletion . SetResult ( r ) ) ;  var  response  =  await  taskCompletion . Task ;  return  JsonConvert . DeserializeObject < String > ( response . Content ) ; 
    }

这是生成方法体的方法:

    private static BlockSyntax GetMethodBody(BaseApiMethodInfo methodInfo, string controllerName)
    {
        var body = new StringBuilder($"var taskCompletion = new TaskCompletionSource<IRestResponse>();{Environment.NewLine}var request = ");
        var dataText = methodInfo.Data != null 
            ? methodInfo.Data.Item2.ToString() 
            : "null";

        var methodParameters = $"\"{controllerName}\", \"{methodInfo.Name}\", {dataText}.ToString()";
        string httpRequestText;
        switch (methodInfo.Method)
        {
            case Method.GET:
                httpRequestText = $"RestSharpExtensions.GetRequest({methodParameters});";
                break;
            case Method.POST:
                httpRequestText = $"RestSharpExtensions.PostRequest({methodParameters});";
                break;
            case Method.PUT:
                httpRequestText = $"RestSharpExtensions.PutRequest({methodParameters});";
                break;
            case Method.DELETE:
                httpRequestText = $"RestSharpExtensions.DeleteRequest({methodParameters});";
                break;
            default:
                throw new ArgumentException("methodInfo.Method");                    
        }

        body.AppendLine(httpRequestText + Environment.NewLine);
        body.AppendLine($"var handle = Client.ExecuteAsync(request, r => taskCompletion.SetResult(r));{Environment.NewLine}");
        body.AppendLine($"var response = await taskCompletion.Task;{Environment.NewLine}");
        body.AppendLine($"return JsonConvert.DeserializeObject<{TypesHelper.GetTypeName(methodInfo.ReturnedType)}>(response.Content);");

        return Block(ParseStatement(body.ToString()));
    }

不幸的是,我找不到任何解决这个问题的要点。这是github repo的参考: https://github.com/ddydeveloper/Roslyn.ApiClient.Codegen

有没有人有这样的问题?

2 个答案:

答案 0 :(得分:0)

致电Formatter.FormatAsync()以运行Roslyn的格式化程序。

答案 1 :(得分:0)

我找到了解决方案。生成方法体的最佳方法是插入List<StatementSyntax>,如下所示:

private static BlockSyntax GetMethodBody(BaseApiMethodInfo methodInfo, string controllerName)
{
    var statements = new List<StatementSyntax>();

    ...

    statements.Add(ParseStatement($"var handle = Client.ExecuteAsync(request, r => taskCompletion.SetResult(r));"));
    statements.Add(ParseStatement($"var response = await taskCompletion.Task;"));
    statements.Add(ParseStatement($"return JsonConvert.DeserializeObject<{TypesHelper.GetTypeName(methodInfo.ReturnedType)}>(response.Content);"));

    return Block(statements);
}

在此之后,您只需将BlockSyntax添加到方法声明中,例如使用.WithBody(body)