【发布时间】:2018-07-04 23:16:36
【问题描述】:
在运行时映射操作期间(例如,当您使用 ResolveUsing 或自定义 TypeConverter 时)是否可以获取源和目标成员的容器类(或至少类型)?
我知道当您将一个对象映射到另一个对象时,这些对象不必是某个“父”或“容器”对象的成员,但我说的是 AutoMapper 递归复制复杂对象的情况对象。
这是一个例子:
我在这里将“A 类”的汽车和船复制(或至少设置)到“B 类”。
public class VehicleCopyProfile : AutoMapper.Profile
{
public VehicleCopyProfile()
{
this.CreateMap<CarA, CarB>();
this.CreateMap<BoatA, BoatB>();
this.CreateMap<WindshieldA, WindshieldB>(
.ConvertUsing((s, d, resContext) =>
{
// *** How can I tell if s is coming from a Car or a Boat? ***
});
}
}
// Cars & Boats each have a Windshield
public class CarA
{
public WindshieldA Windshield {get;set;}
}
public class BoatA
{
public WindshieldA Windshield {get;set;}
}
public class WindshieldA
{
public string Name {get;set;}
}
public class CarB
{
public WindshieldB Windshield {get;set;}
}
public class BoatB
{
public WindshieldB Windshield {get;set;}
}
public class WindshieldB
{
public string Name {get;set;}
}
【问题讨论】:
-
对不起,我不知道你在问什么。
-
现在好点了吗? ...
-
你不能。但是你可以通过 context.Items 传递根对象。
标签: c# .net automapper