I am somewhat new to Generics and I came across this issue, I have repetitive code that I am trying to clean up. The signature is different but the code being executed is the same, is there a way to pass in a generic type instead of having to specify each type in a new signature?
public JsonResult<JsonData> GetServiceData(Func<IServiceResponse<IEnumerable<Order>>> func)
{
var response = func();
var jsonDataContainer = Mapper.Map<JsonData>(response);
var result = GetJsonResult(jsonDataContainer);
return result;
}
public JsonResult<JsonData> GetServiceData(Func<IServiceResponse<List<int>>> func)
{
var response = func();
var jsonDataContainer = Mapper.Map<JsonData>(response);
var result = GetJsonResult(jsonDataContainer);
return result;
}
public JsonResult<JsonData> GetServiceData(Func<IServiceResponse<User>> func)
{
var response = func();
var jsonDataContainer = Mapper.Map<JsonData>(response);
var result = GetJsonResult(jsonDataContainer);
return result;
}
It's hard to answer this question definitively, because you haven't specified the signature of Mapper.Map
.
But, if Mapper.Map
can take an IServiceResponse<T>
of any type T
, then this would work.
public JsonResult<JsonData> GetServiceData<T>(Func<IServiceResponse<T>> func)
{
IServiceResponse<T> response = func();
var jsonDataContainer = Mapper.Map<JsonData>(response);
var result = GetJsonResult(jsonDataContainer);
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.