我有两个类,使用自动映射器将一个映射到另一个。例如:
使用AutoMapper进行这种操作是否可行呢?
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>();
它运行正常,但有时类Source
中成员Type
会变为null
。在这种情况下,我想从TypeId
属性将类Destination
中的成员Type
映射过来。这就是我想要的。
if Source.Type != null
then map Destination.Type from it
else map it as
Destination.Type = new IdNameDest { Id = Source.Id }
使用AutoMapper进行这种操作是否可行呢?