I am trying to create a function like
public static TResponse Run<TService, TResponse>(Controller mvcController, IServiceController serviceController, Func<TService, TResponse> action, bool suppressErrors)
where TService : ICommunicationObject
where TResponse : ResponseBase<TResponse>
{
TResponse response = serviceController.Run<TService, TResponse>(action);
if (!suppressErrors)
response.Result.Errors.ToList().ForEach(i => mvcController.ModelState.AddModelError(UniqueKey.ValidationMessage, i.Message));
return response;
}
and class has been defined as
[DataContract]
public class ResponseBase<T> where T: new()
{
public ResponseBase()
{
Result = new Result<T>();
}
[DataMember]
public Result<T> Result { get; set; }
}
I am getting the compilation error as TResponse must be a non abstract type with a parameterless constructor in order to use it as parameter TResponse
Any help would be appreciated..
Although you’ve defined the
new()constraint forTinResponseBase<T>, the compiler requires you to declare the same constraint in other classes that useResponseBase<T>generically.All you have to do is add the
new()constraint toTResponsein your method: