I'm not sure, why calling Map2
gives me
The type arguments for method 'Program.Map2(object, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Here is the code...
void Test()
{
var test1 = Map1(1, MapIntToString);
var test2 = Map2(1, MapIntToString);
}
To Map1<From, To>(From value, Func<From, To> mapFunc) => mapFunc(value);
To Map2<From, To>(object value, Func<From, To> mapFunc) => mapFunc((From)value);
string MapIntToString(int value) => Convert.ToString(value);
This is very simplified example. I need to convert some lists of DTOs to Models (and back), but it should be same case...
Map1
, you easily let it infer theFrom
isint
since that's the type of the first parameter. InMap2
, it doesn't know where to start withFrom
– Damien_The_UnbelieverMap1
andMap2
, the compiler has no information about how those methods are implemented - it can only work from the method signatures - and even if it could do deeper inspection, the cast is in the wrong direction. – Damien_The_Unbeliever