AutoMapper determine what to map based on generic type
- by Daz Lewis
Hi,
Is there a way to provide AutoMapper with just a source and based on the specified mapping for the type of that source automatically determine what to map to?
So for example I have a type of Foo and I always want it mapped to Bar but at runtime my code can receive any one of a number of generic types.
public T Add(T entity)
{
//List of mappings
var mapList = new Dictionary<Type, Type> {
{typeof (Foo), typeof (Bar)}
{typeof (Widget), typeof (Sprocket)}
};
//Based on the type of T determine what we map to...somehow!
var t = mapList[entity.GetType()];
//What goes in ?? to ensure var in the case of Foo will be a Bar?
var destination = AutoMapper.Mapper.Map<T, ??>(entity);
}
Any help is much appreciated.