Automapper DynamicMap not displaying lists of anonymous types

I have the following code snippet.

var files = query.ToList();
var testFile = Mapper.DynamicMap<EftFileDto>(files.First());
var filesDto = Mapper.DynamicMap<List<EftFileDto>>(files);

testFile has a correctly displayed value, but filesDto is empty.

Does dynamicMap seem to work with individual elements but not lists?

files is a list of anonymous objects.

EDIT : it does not work if I also use arrays. I can make it work, but ...

        var filesDto = query.Select(Mapper.DynamicMap<EftFileDto>).ToList();
+5
source share
1 answer

In most mapping scenarios, we know that the type was mapped at compile time. In some cases, the source type is not known until especially in scenarios where Im uses dynamic types or in extensibility scripts.

DynamicMap . , AutoMapper ( DynamicMap ).

: http://lostechies.com/jimmybogard/2009/04/15/automapper-feature-interfaces-and-dynamic-mapping/

: DynamicMap CreateMap, .

Person

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
    }

, .

var persons = new List<Person>();
for (int i = 0; i < 100; i++)
{
      persons.Add(new Person { 
                Name = String.Format("John {0}", i), 
                Surname = String.Format("Smith {0}", i), 
                Age = i });
}

, .

var anonymousTypes = persons.Select(p => new { 
            p.Name, 
            p.Surname, 
            FullName = String.Format("{0}, {1}", p.Surname,p.Name) }).ToList();

var testFile = Mapper.DynamicMap<Person>(anonymousTypes.First()); 

,

var testFiles  = anonymousTypes.Select(Mapper.DynamicMap<Person>).ToList(); 
+7

All Articles