using AutoMapper;
public class Source
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap()
.ForAllOtherMembers(opt => opt.Ignore()); // 忽略目标对象中不存在的属性
}
}
public class Program
{
public static void Main(string[] args)
{
var source = new Source
{
Id = 1,
Name = "Test",
Description = "This is a test"
};
var configuration = new MapperConfiguration(cfg => cfg.AddProfile());
var mapper = configuration.CreateMapper();
var destination = mapper.Map(source);
Console.WriteLine($"Id: {destination.Id}");
Console.WriteLine($"Name: {destination.Name}");
}
}