你需要使用
Mapper.DynamicMap<TDest>(source)
地图。
正如您在下面的示例中所看到的,它会自动将匹配的属性从源映射到目标。
using AutoMapper;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var source = new Foo {Value = "Abc"};
var destination = Mapper.DynamicMap<FooViewModel>(source);
Debug.Assert(source.Value == destination.Value);
}
}
public class Foo
{
public string Value { get; set; }
}
public class FooViewModel
{
public string Value { get; set; }
}