我面臨的Automapper自定義類型轉換,其中ITypeConverter接口已經改變過時的文件後,我在這裏找到了答案: ITypeConverter interface has been changed in AutoMapper 2.0,我是能夠使產生轉換類型的一個工作原型如下:
public class ComplexTypeConverter : ITypeConverter<ComplexSourceType, ComplexDestinationType>
{
public ComplexDestinationType Convert(ResolutionContext context)
{
var source = (ComplexSourceType)context.SourceValue;
return new ComplexDestinationType
{
MyProperty = Mapper.Map<SourceType, DestinationType>(source.MyProperty),
ValueComputed = source.ValueToBeComputed + 10
};
}
}
public class TypeConverter : ITypeConverter<SourceType, DestinationType>
{
public DestinationType Convert(ResolutionContext context)
{
var source= (SourceType)context.SourceValue;
return new DestinationType
{
ValueComputed1 = source.ValueToBeComputed1 + 10,
ValueComputed2 = source.ValueToBeComputed2 + 10
};
}
}
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => {
cfg.CreateMap<ComplexSourceType, ComplexDestinationType>().ConvertUsing(new ComplexTypeConverter());
cfg.CreateMap<SourceType, DestinationType>().ConvertUsing(new TypeConverter());
});
Mapper.AssertConfigurationIsValid();
ComplexSourceType source = new ComplexSourceType
{
MyProperty = new SourceType
{
ValueToBeComputed1 = 1,
ValueToBeComputed2 = 1
},
ValueToBeComputed = 1
};
var dest = Mapper.Map<ComplexSourceType, ComplexDestinationType>(source);
}
}
的DEST對象持有11上的每個屬性
當你試圖調用它發生了什麼修改的數據? – Rhumborl
我還沒有打過電話,我只是想知道是否有可能。 – meJustAndrew
我成功創建了轉換器並調用了Mapper.Map()方法。對於低的問題抱歉! – meJustAndrew