【发布时间】:2015-04-22 10:40:32
【问题描述】:
我想知道是否有办法使用一些自定义类型或值解析器来处理这种情况。
public class SuperDateTime
{
public DateTimeOffset Date { get; set; }
public string Timezone { get; set; }
}
public class Entity
{
public DateTimeOffset CreationDate { get; set; }
public string CreationDateZone { get; set; }
public DateTimeOffset EndDate { get; set; }
public string EndDateZone { get; set; }
}
public class Model
{
public SuperDateTime CreationDate { get; set; }
public SuperDateTime EndDate { get; set; }
}
当我在目标对象中有一个SuperDateTime 时,我想用源对象中的关联DateTimeOffset 和时区string 来实例化这个对象。
当然,我想做的是做一些通用的东西,所以不要想到每个CreateMap 中的每个CreateMap 中的MapFrom
我尝试使用自定义 TypeConverter 来实现,但它仅支持 SourceType -> DestinationType
就我而言,我有一个 string 和 DateTimeOffset 必须创建一个 SuperDateTime
【问题讨论】:
-
除了我在下面的回复之外,定义用于源类型的结构(例如 SuperDateTimeDto)是否有任何价值?然后,您可以定义一个从结构到目标 SuperDateTime 的自定义值解析器。
标签: c# automapper