AutoMapper determine what to map based on generic type
Posted
by
Daz Lewis
on Stack Overflow
See other posts from Stack Overflow
or by Daz Lewis
Published on 2011-03-17T00:07:02Z
Indexed on
2011/03/17
0:10 UTC
Read the original article
Hit count: 222
automapper
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.
© Stack Overflow or respective owner