Hcf AutoMapper 对象映射组件,封装基于AutoMapper的对象映射实现
通过nuget引用 Hcf.AutoMapper 程序集
Install-Package HcfNet.AutoMapper
添加AutoMapper组件包
services.AddPack<AutoMapperPack>();
克隆对象
using Hcf.AutoMapper;
using Hcf.Mapping;
TestInfo testInfo1 = new TestInfo()
{
Id = 1,
Name = Guid.NewGuid().ToString(),
CreatedTime=DateTime.Now
};
var testInfo2 = testInfo1.MapTo<TestInfo>();
对象动态赋值
创建一个Dto类,标记可以从TestInfo类型映射
/// <summary>
/// TestOutputDto
/// </summary>
[MapFrom(typeof(TestInfo))]
public class TestOutputDto
{
/// <summary>
/// Id
/// </summary>
public int Id { set; get; }
/// <summary>
/// 名称
/// </summary>
public string Name { set; get; }
}
将testInfo1对象的值赋值给该Dto对象
var testInfo3 = testInfo1.MapTo<TestOutputDto>();