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();