1
我有兩個類並使用Automapper將其映射到另一個類。例如:Automapper。如果源成員爲null,則映射
public class Source
{
// IdName is a simple class containing two fields: Id (int) and Name (string)
public IdName Type { get; set; }
public int TypeId {get; set; }
// another members
}
public class Destination
{
// IdNameDest is a simple class such as IdName
public IdNameDest Type { get; set; }
// another members
}
然後我用Automapper映射Source
到Destination
:
cfg.CreateMap<Source, Destination>();
它工作正常,但有時成員Type
類Source
變得null
。在這些情況下,我想從TypeId
屬性映射成員Type
類Destination
。這就是我想要的:
if Source.Type != null
then map Destination.Type from it
else map it as
Destination.Type = new IdNameDest { Id = Source.Id }
AutoMapper有可能嗎?
https://stackoverflow.com/a/9205604/34092有幫助嗎? – mjwills