I have the following helper method:
public static T CreateRequest<T>()
where T : Request, new()
{
T request = new T();
return request;
}
I want to use this method from inside another method in another helper:
public T Map<F, T>(F value, T toValue)
where T : new()
where F : new()
{
if (typeof(T).BaseType.FullName == "MyNamespace.Request")
{
toValue = MyExtensions.CreateRequest<T>();
}
else
{
toValue = new T();
}
}
But then I get the error:
Type "T" cannot be used as a parameter of type "T" in the generic type or method "MyExtensions.CreateRequest ()". There is no box conversion or type parameter conversion from 'T' to 'MyNamespace.Request'.
Is there a way to apply type “T” so that CreateRequest uses it without problems?
EDIT:
I know I can do two things:
- relax restrictions on CreateRequest or
- tighten the contours in the map.
, CreateRequest Request, , ( Request) Map.