I would like to know how to map a list with ‘keyvaluepair’ including properties to an object.
Would be nice to resolve it with AutoMapper, but any suggestions would be very kind.
Here’s how I would like it to work with FooBar that got properties and a list of key/values that depending on the properties matching with FooBars properties should be mapped:
// AutoMapper
var mappedFooBar = Mapper.Map<IEnumerable<KeyValuePair<string, object>> FooBar>(fooBar);
I have been struggling with this for a while now so any suggestions would be nice.
Also tried to map with reflection, but cannot seem to get it to work.
Thanks in advance.
Here is a simple, straight-forward reflection solution. It doesn’t do type checking, etc., but it could be easily built in:
Of course, you could create an AutoMapper
ITypeConverterthat uses this. If you already use AutoMapper, this might be a good idea to keep the code consistent.Be aware that this reflection technique might be slow – if you need to map this often, caching the resolved setters per type will be beneficial to performance.