简体   繁体   English

AutoMapper和其他对象

[英]AutoMapper and different objects

I have list of objects like the following: 我有如下对象列表:

A: IBaseType
{
   string X
   DateTime Y
   int Z
}

and I want to get after mapping three different objects that are produced based on their properties. 我想在映射根据其属性生成的三个不同对象后得到。

For instance: 例如:

Mapper.Map<IList<A>, IList<IBaseType>>(list); 

and in output collection get different objects 在输出集合中得到不同的对象

X: IBaseType
{
  string X;
}

Y: IBaseType
{
   DateTime Y
}

and so on. 等等。

Or in something like this: 或者像这样:

Mapper.CreateMap<Item, ItemModel>().FromMap(d=>d.Conditions, opt=>(there some like to IValueResovler );

I think that best way is to implement custom mapping functionality. 我认为最好的方法是实现自定义映射功能。

Something like this: 像这样:

public IBaseType Map(A item)
{
     if ( /*your condition*/ )
          return new X(){ X = item.X}

     else if ( /*your condition*/ )
          return new Y(){ Y = item.X}
}

and use this method 并使用这种方法

List<IBaseType> result = new List<IBaseType>() ;
list.Foreach(x => result.Add(Map(x)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM