我正在使用4.5库,我想在函数的特定位置调用函数pass作为参数。
当前作为参数传递的方法在传递参数的方法之前被调用。
这是我当前的代码
ApiCall<EntityMessage>.CallApi(token, ServicesMessage.Get(messageId));
public static HttpResponseMessage CallApi(string token, T method)
{
HttpResponseMessage response = ServicesAPIAuth.CheckToken(token);
if (response.StatusCode == HttpStatusCode.OK)
{
//want to call the method passed here
response = ServicesJsonWebApi.FormatJsonApiResponse(method, HttpStatusCode.OK);
}
return response;
}
所以我想在写评论的地方打电话给ServicesMessage.Get(messageId)
答案 0 :(得分:1)
您真正要寻找的是Func<T>
代表
public static HttpResponseMessage CallApi(string token, Func<T> method) {
HttpResponseMessage response = ServicesAPIAuth.CheckToken(token);
if (response.StatusCode == HttpStatusCode.OK) {
//call the method passed here
response = ServicesJsonWebApi.FormatJsonApiResponse(method(), HttpStatusCode.OK);
}
return response;
}
然后像这样称呼
ApiCall<EntityMessage>.CallApi(token, () => ServicesMessage.Get(messageId));