Trying to figure out why I can't do this...
I have this simple interface "IMapper" for changing one object into another like so:
public interface IMapper<T>
{
T Map();
}
And you just call it like this:
var newObject = oldObject.Map();
And then I decided that I should extend IEnumerable so that I can do this over an entire enumeration. So I created this function:
public static class EnumerableMapperExtension
{
public static IEnumerable<TTo> Map(this IEnumerable<TFrom> enumerable) where TFrom : IMapper<TTo>
{
return enumerable.Select(x => x.Map());
}
}
Except that the above syntax is wrong. It has to look like this:
public static class EnumerableMapperExtension
{
public static IEnumerable<TTo> Map<TTo, TFrom>(this IEnumerable<TFrom> enumerable) where TFrom : IMapper<TTo>
{
return enumerable.Select(x => x.Map());
}
}
So instead of this nice looking call...
IEnumerable<NewType> newArray = oldArray.Map();
I have to call it like this:
IEnumerable<NewType> newArray = oldArray.Map<NewType, Oldtype>();
Is there any reason the compiler can't figure out the types the enumerables store?
(edited for clarity)