how to map an anonymous object to a class by AutoMapper?

Update 2019-07-31: CreateMissingTypeMaps is now deprecated in AutoMapper v8, and will be removed in v9. Support for automatically created maps will be removed in version 9.0. You will need to explicitly configure maps, manually or using reflection. Also consider attribute mapping. Update 2016-05-11: DynamicMap is now obsolete. Now you need to create a mapper from … Read more

A dictionary where value is an anonymous type in C#

You can’t declare such a dictionary type directly (there are kludges but these are for entertainment and novelty purposes only), but if your data is coming from an IEnumerable or IQueryable source, you can get one using the LINQ ToDictionary() operator and projecting out the required key and (anonymously typed) value from the sequence elements: … Read more

Merging anonymous types

So here’s, what I finally came up with (inspired by @BlueMonkMN’s answer): public dynamic Merge(object item1, object item2) { if (item1 == null || item2 == null) return item1 ?? item2 ?? new ExpandoObject(); dynamic expando = new ExpandoObject(); var result = expando as IDictionary<string, object>; foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties()) { result[fi.Name] = fi.GetValue(item1, … Read more

Casting anonymous type to dynamic

Anonymous objects are internal, which means their members are very restricted outside of the assembly that declares them. dynamic respects accessibility, so pretends not to be able to see those members. If the call-site was in the same assembly, I expect it would work. Your reflection code respects the member accessibility, but bypasses the type’s … Read more

Anonymous Type vs Dynamic Type

You seem to be mixing three completely different, orthogonal things: static vs. dynamic typing manifest vs. implicit typing named vs. anonymous types Those three aspects are completely independent, they have nothing whatsoever to do with each other. Static vs. dynamic typing refers to when the type checking takes place: dynamic typing takes place at runtime, … Read more