我有三个名为Employee、EmployeeTwo和EmployetThree的类,我想将Employee-to EmployeyTwo和EmployeeThree映射。
以下是课程。我还尝试在Employee Class属性上定义AdaptMember属性,但它只定义了一个,我必须用两个不同的属性来映射它。
注意:我不能为EmployeeTwo和EmployetThree类定义任何属性,因为它来自API,并且可以是regenrate。
public class Employee
{
[AdaptMember(nameof(EmployeeTwo.EmployeeID))]
public int ID { get; set; }
[AdaptMember(nameof(EmployeeTwo.EmployeeName))]
public string Name { get; set; }
}
public class EmployeeTwo
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
public class EmployeeThree
{
public int EmployeeThreeID { get; set; }
public string EmployeeThreeName { get; set; }
}
任何帮助都会被告知。
据我所知,您希望使用mapster进行自定义对象映射。如果你想进行自定义映射,你需要创建一个类:
using Mapster;
namespace Application.Mapsters
{
public class Config : ICodeGenerationRegister
{
public void Register(CodeGenerationConfig config)
{
TypeAdapterConfig<Employee, EmployeeTwo>
.NewConfig()
.Map(dst => dst.EmployeeID, src => src.ID)
.Map(dst => dst.EmployeeName, src => src.Name);
}
}
}
例如,按照这个例子来了解如何使用mapster:进行自定义映射
https://floatincode.net/2021/07/26/mapster-generate-dto-async-after-map-actions-dependency-injection/