[英]Dynamically returning serialized RestSharp results
Trying to make a generic method to call REST endpoints using RestSharp the only issue I'm having is returning the content as dynamic . 尝试使用RestSharp调用REST端点的通用方法,我遇到的唯一问题是将内容作为动态返回。
Response.Data is null and Response.Content is always a string. Response.Data为null,Response.Content始终为字符串。 I would like the return value to be at least an anonymous type. 我希望返回值至少是一个匿名类型。
public T Call<T>(string url, Method method = Method.GET, ResponseType type = ResponseType.JSON, params Parameter[] parameters)
{
var request = new RestRequest(url, method);
switch (type)
{
case ResponseType.JSON:
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
break;
case ResponseType.XML:
request.AddHeader("Content-Type", "application/xml");
request.AddHeader("Accept", "application/xml");
request.RequestFormat = DataFormat.Xml;
break;
}
foreach (var parameter in parameters)
request.AddParameter(parameter);
var response = _client.Execute<dynamic>(request);
if (!string.IsNullOrEmpty(response.ErrorMessage) || response.ErrorException != null)
throw new System.Exception(response.ErrorMessage, response.ErrorException);
return response.Data;
}
鉴于你有类型T,你不能这样做:
var response = _client.Execute<T>(request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.