IDictionary<string, object> to class mapping

1,888 views
Skip to first unread message

LOBOMINATOR

unread,
Apr 14, 2010, 4:01:34 PM4/14/10
to AutoMapper-users
Hello
Is it possible to map for example

Dictionary<string, object> keyValuePairs = new Dictionary<string,
object> {
{ "Name", "LOBOMINATOR" },
{ "Age", 25 },
...
}

to public class Person {

public string Name { get; set; }
public int Age { get; set; }
}

?

Thanks for your help

Jimmy Bogard

unread,
Apr 15, 2010, 12:01:02 PM4/15/10
to automapp...@googlegroups.com
Not right now, and some people have coded a custom way to do this with a custom IObjectMapper.  It's something I'm looking at for the next major version, however.

Do you have any other scenarios around dictionaries?  Arrays, child objects, etc?  That would help for me when I build this feature.

Thanks,

Jimmy


--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To post to this group, send email to automapp...@googlegroups.com.
To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/automapper-users?hl=en.


LOBOMINATOR

unread,
Apr 26, 2010, 4:00:11 PM4/26/10
to automapp...@googlegroups.com

Hy Jimmy,

Sorry for the late response. I would use this mainly for generic configuration dictionary mapping to concrete configuration types.

 

Daniel

Anton Platzoeder

unread,
Oct 3, 2014, 2:49:40 AM10/3/14
to automapp...@googlegroups.com
Hi Jimmy,

Has this been implemented yet in Automapper?


Anton

Jimmy Bogard

unread,
Oct 3, 2014, 8:46:00 AM10/3/14
to automapper-users
Nope. But hey, I'm always open for pull requests :)

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sean.p...@gmail.com

unread,
Feb 26, 2015, 9:55:22 PM2/26/15
to automapp...@googlegroups.com
I've done my research and trial and error, and with less hair I thought I'd turn it over to the gurus -- is a Dictionary to ICollection possible with AutoMapper 3.3.1?

I get an AutoMapper.AutoMapperConfigurationException when validating the following reverse mapping:

    Mapper.CreateMap<EntityClass, DTO>()
        .ForMember(dest => dest.MySettings, opts => opts.MapFrom(src => src.MySettings.ToDictionary(setting => setting.TypeId, setting => setting)))
    .ReverseMap()
        .ForMember(dest => dest.MySettings, opts => opts.MapFrom(src => ((Dictionary<int, SettingDTO>)src.MySettings).Values))

I'm trying to pull user settings from the database (via EntityFramework 6) and nestle them in with the DTO - which is sent out OK via HTTP GET. On the way back, it's breaking while converting the JSON array of SettingDTOs back into their Entities. Validating the config gives me:

AutoMapper.AutoMapperConfigurationException:

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
==================================================
DTO-> EntityClass(Source member list)
DTO -> EntityClass (Source member list)

Unmapped properties:
Settings

I'd really appreciate any help or advice. Thanks all!
Reply all
Reply to author
Forward
0 new messages