https://www.jianshu.com/p/419a3b7f12d5

列表和数组(Lists and Arrays)

AutoMapper只需要配置元素类型的映射配置,不需要针对列表和数组进行配置。举个例子,我们可能有以下源类型和目标类类型:

public class Source
{
public int Value { get; set; }
} public class Destination
{
public int Value { get; set; }
}

所有的基础泛型集合类型都支持:

Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());

var sources = new[]
{
new Source { Value = 5 },
new Source { Value = 6 },
new Source { Value = 7 }
}; IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources);
ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources);
IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);
List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources);
Destination[] arrayDest = Mapper.Map<Source[], Destination[]>(sources);

换句话说,源集合类型支持以下几种:

  • IEnumerable
  • IEnumerable<T>
  • ICollection
  • ICollection<T>
  • ILsit
  • IList<T>
  • List<T>
  • Arrays

对于非泛型的枚举类型,仅支持未映射的可分配类型,因为AutoMapper不能推算出你想尝试映射的类型。如上例所示,没有必要显式配置列表类型,只需要配置其成员类型就可以了。

当映射一个已存在的集合时,会先清空目标集合。如果这不是你想要的,那么请查看
AutoMapper.Collection

多态元素集合

很多时候,我们的源类型和目标类型都会继承至其它类型,AutoMapper支持多态数组和集合,如果存在对应的派生类型则使用

public class ParentSource
{
public int Value1 { get; set; }
} public class ChildSource : ParentSource
{
public int Value2 { get; set; }
} public class ParentDestination
{
public int Value1 { get; set; }
} public class ChildDestination : ParentDestination
{
public int Value2 { get; set; }
}

AutoMapper仍然需要显式配置子映射, 因为AutoMapper不知道要使用哪一个子目标映射配置。下面举个例子:

Mapper.Initialize(c=> {
c.CreateMap<ParentSource, ParentDestination>()
.Include<ChildSource, ChildDestination>();
c.CreateMap<ChildSource, ChildDestination>();
}); var sources = new[]
{
new ParentSource(),
new ChildSource(),
new ParentSource()
}; var destinations = Mapper.Map<ParentSource[], ParentDestination[]>(sources); destinations[0].ShouldBeInstanceOf<ParentDestination>();
destinations[1].ShouldBeInstanceOf<ChildDestination>();
destinations[2].ShouldBeInstanceOf<ParentDestination>();

07.AutoMapper 之列表和数组(Lists and Arrays)的更多相关文章

  1. perl5 第四章 列表和数组变量

    第四章 列表和数组变量 by flamephoenix 一.列表二.数组--列表的存贮  1.数组的存取  2.字符串中的方括号和变量替换   3.列表范围  4.数组的输出  5.列表/数组的长度  ...

  2. Perl的列表和数组

    列表和数组 列表 使用括号包围的元素,括号中的元素使用逗号隔开的是列表. 列表中的元素可以是字符串.数值.undef或它们的混合. 列表中的字符串元素需要使用引号包围. 空列表是括号中什么都没有的列表 ...

  3. Spring MVC 处理列表和数组数据

    SpringMVC处理列表和数组类型: 当form提交多个参数时,可以使用@RequestParam(value="")注解将其注入到列表或数组中,否则参数无法传值 1 contr ...

  4. python3.4学习笔记(十一) 列表、数组实例

    python3.4学习笔记(十一) 列表.数组实例 #python列表,数组类型要相同,python不需要指定数据类型,可以把各种类型打包进去#python列表可以包含整数,浮点数,字符串,对象#创建 ...

  5. Perl 学习笔记-列表和数组

    笔记来自<<Perl语言入门第5版>> 1. Perl中列表指标量的有序集合,数组则是存储列表的变量, 这两个术语经常混用,不过更精确地说,列表指数据,而数组指变量.数组的表示 ...

  6. perl第三章 列表和数组

    访问数组中的元素    $fred[0]   $fred[1] $number=2.75; print $fred[$number-1]  结果就是print $fred[1] 特殊的数组索引1.对索 ...

  7. .NET重思(三)-数组列表与数组的区别,栈集合和队列结合的区别

    数组列表和数组十分相似,区别在于数组列表的容量是可以动态变化的,而数组的容量是固定的.数组即Array类,数组列表即ArrayList类,两者十分相似.不过,Array类在System命名空间下,Ar ...

  8. 对Python中列表和数组的赋值,浅拷贝和深拷贝的实例讲解

    引用:https://www.jb51.net/article/142775.htm 列表赋值: 1 2 3 4 5 6 7 >>> a = [1, 2, 3] >>&g ...

  9. FastReport.Net中使用列表和数组作为报表数据源

    大多数现代报告工具允许您使用几乎任何数据库,然而,并不是所有报表工具都能以一个数据源的列表或数组来工作.本文中将展示如何使用FastReport .Net报表工具来实现. 请注意以下重要几点: 清单中 ...

随机推荐

  1. 15.Python bool布尔类型

    Python 提供了 bool 类型来表示真(对)或假(错),比如常见的5 > 3比较算式,这个是正确的,在程序世界里称之为真(对),Python 使用 True 来代表:再比如4 > 2 ...

  2. centos 系统时间设置

    在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况.如果没有安装,而你使用的是 CentOS系统 那使用命令 yum insta ...

  3. 2018-2019-2 网络对抗技术 20165232 Exp 8 Web基础

    2018-2019-2 网络对抗技术 20165232 Exp 8 Web基础 原理与实践说明 1.实践内容概述 Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与 ...

  4. tp5获取器的用法。

    1.命名规则   get + 属性名的驼峰命名+ Attr      ------>在相应的model中创建方法 例如: protected function getSexAttr($value ...

  5. redhat7.2配置yum源

      版权声明:声明:本博客欢迎转发,但请保留原作者信息!本站文章皆基于公开来源信息,仅代表作者个人观点 https://blog.csdn.net/wylfengyujiancheng/article ...

  6. oracle字段like多个条件

    写oracle sql时有时候会有 and (字段 like ‘匹配串1’or 字段 like ‘匹配串2’or ...)这样的情况出现,下面提供一个简洁点的解决方案: and REGEXP_LIKE ...

  7. 对保存的参数checkpoints进行可视化读取 1.pywrap_tensorflow.NewCheckpoint(获得checkpoint的读取器) 2.np.save(对npy文件进行保存) 3.tl.file.load_npy_to_any(对保存的npy文件进行读取)

    1. pywrap_tensorflow.NewCheckpoint(path)获得checkpoint的读取器 参数说明: path表示checkpoint的路径 2.np.save(path, d ...

  8. koa2中间键原理

    一.koa2 const http = require('http'); const compose = require('./compose'); class Koa { constructor() ...

  9. leetcode 116填充每个节点的下一个右侧节点指针

    time O(n) ,sapce O(n) /* // Definition for a Node. class Node { public: int val; Node* left; Node* r ...

  10. idea 导入 open项目

    导入  import Project 选择你的项目目录 (位置可以直接是svn下下来的项目目录 不用新建空项目或者目录) 如果有提示直接yes  没有拉到 一路next 如果提示  覆盖就ok (一路 ...