如何将具有不同数据类型的`Object`类型的属性映射到相应的classtype?

时间:2015-09-21 07:11:25

标签: c# automapper

我有两个班级:

public class A{
[System.Xml.Serialization.XmlElementAttribute("type1", typeof(XMLItemType1), IsNullable=true)]
[System.Xml.Serialization.XmlElementAttribute("type2", typeof(XMLItemType2), IsNullable=true)]
public object Item { get; set; }
}

public class B{
public object Item { get; set; }
}

映射创建如下:

AutoMapper.Mapper.CreateMap<A, B>()
.ForMember(domainObject => domainObject.Item ,
metaData => metaData.MapFrom(xmlObject => xmlObject.Item ));


AutoMapper.Mapper.CreateMap<XMLItemType1, XMLObjectType1>();
AutoMapper.Mapper.CreateMap<XMLItemType2, XMLObjectType2>();

AutoMapper.Mapper.Map<A, B>(data);            

但是B类Item的输出属性是XMLItemType1类型,而不是XMLObjectType1

如何确保映射Item的类型为XMLObjectType1XMLObjectType2后?

0 个答案:

没有答案