开发者

EmitMapper: Map array of derived classes

开发者 https://www.devze.com 2023-04-01 19:03 出处:网络
Let\'s assume that we have following class hierarchy class DtoBaseClass { public string BaseProperty {get; set;}

Let's assume that we have following class hierarchy

class DtoBaseClass
{ 
     public string BaseProperty {get; set;}
}

class A1 : DtoBaseClass
{ 
     public string SomeProperty {get; set;}
}

class A2 : A
{ 
     public string AnotherProperty {get; set;}
}

class DomainClass
{ 
     public string BaseProperty {get; set;}
}

class B1 : DomainClass
{ 
     public string SomeProperty {get; set;}
}

class B2 : DomainClass
{ 
     public string AnotherProperty {get; set;}
}

Also I have a service which returns an array of DtoBaseClass which really contains instances of A1 and A2. How should I map this array to array of DomainClass instances and save relation between B1 <-> A1, B2 <-> B1?

In default configuration I'll get an array开发者_C百科 of DomainClass and loose all properties of derived classes.

I use this code for mapping:

var dtoToDomainMapper =
ObjectMapperManager.DefaultInstance.GetMapper<DtoClasss[], DomainClass>();

var dtoObjects = externalService.GetObjects();
return dtoToDomainMapper.Map(dtoObjects);
0

精彩评论

暂无评论...
验证码 换一张
取 消