How to map property of type `Object` with different datatypes to a corresponding classtype?

梦想与她 提交于 2019-12-11 10:51:08

问题


I have two classes:

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; }
}

Mapping is created as follows:

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);            

But the output property of B class Item is of type XMLItemType1 instead of XMLObjectType1.

How can I make sure that after mapping Item is of type XMLObjectType1 or XMLObjectType2?

来源:https://stackoverflow.com/questions/32689294/how-to-map-property-of-type-object-with-different-datatypes-to-a-corresponding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!