Я работаю над библиотекой 4.5, и я хочу вызвать передачу функции как параметр в определенном месте моего метода.
В настоящее время метод, переданный как параметр, вызывается перед тем, в котором я передаю параметр.
Вот мой текущий код
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), где я написал комментарий





На самом деле вы ищете делегата 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));