问题
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