I am using .NET mapping library AutoMapper in my application, and I have a generic extension method like this:
public static T2 Map<T1, T2>(this T1 o)
{
return Mapper.Map<T1, T2>(o);
}
...
var nc = new NonCustomer();
Customer c = nc.Map<NonCustomer, Customer>();
Is there any way I can get rid of the T1 generic parameter from the extension method so it is inferred? Resulting in a call like this:
var nc = new NonCustomer();
Customer c = nc.Map<Customer>();
You do not need to use generic version for T1 parameter.
You just need to change it to object:
If you are using AutoMapper v2 and above, you could write it as following: