namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//1.普通转换
Name name1 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.BeforeMap((name, nameDto) => Console.WriteLine("hello world before"))
.AfterMap((name, nameDto) => Console.WriteLine("hello world after"));
NameDto nameDto1 = Mapper.Map<Name, NameDto>(name1);
Console.WriteLine("");
Console.WriteLine(nameDto1.FirstName + nameDto1.LastName);
Console.WriteLine();
//Console.ReadKey(); //整体设置
//2.整体即时转换
Mapper.Reset();
Name name2 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ConstructUsing(name => new NameDto() { AllName = name.FirstName + name.LastName });
NameDto nameDto2 = Mapper.Map<Name, NameDto>(name2);
Console.WriteLine("");
Console.WriteLine(nameDto2.AllName);
Console.WriteLine();
//Console.ReadKey(); //3.整体通过TypeConverter类型转换
Mapper.Reset();
Name name3 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ConvertUsing<NameConverter>();
NameDto nameDto3 = Mapper.Map<Name, NameDto>(name3);
Console.WriteLine("");
Console.WriteLine(nameDto3.AllName);
Console.WriteLine();
//Console.ReadKey(); //单属性设置
//4.属性条件转换
Mapper.Reset();
Name name4 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.FirstName, opt => opt.Condition(name => !name.FirstName.Equals("l", StringComparison.OrdinalIgnoreCase)));
NameDto nameDto4 = Mapper.Map<Name, NameDto>(name4);
Console.WriteLine("");
Console.WriteLine(string.IsNullOrEmpty(nameDto4.FirstName));
Console.WriteLine();
//Console.ReadKey(); //5.属性忽略
Mapper.Reset();
Name name5 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.FirstName, opt => opt.Ignore());
NameDto nameDto5 = Mapper.Map<Name, NameDto>(name5);
Console.WriteLine("");
Console.WriteLine(string.IsNullOrEmpty(nameDto5.FirstName));
Console.WriteLine();
//Console.ReadKey(); //6.属性转换
Mapper.Reset();
Name name6 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.AllName, opt => opt.MapFrom(name => name.FirstName + name.LastName));
NameDto nameDto6 = Mapper.Map<Name, NameDto>(name6);
Console.WriteLine("");
Console.WriteLine(nameDto6.AllName);
Console.WriteLine();
//Console.ReadKey(); //7.属性通过ValueResolver转换
Mapper.Reset();
Name name7 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, StoreDto>()
.ForMember(storeDto => storeDto.Name, opt => opt.ResolveUsing<NameResolver>());
StoreDto store1 = Mapper.Map<Name, StoreDto>(name7);
Console.WriteLine("");
Console.WriteLine(store1.Name.FirstName + store1.Name.LastName);
Console.WriteLine();
//Console.ReadKey(); //8.属性填充固定值
Mapper.Reset();
Name name8 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.AllName, opt => opt.UseValue<string>("ljzforever"));
NameDto nameDto8 = Mapper.Map<Name, NameDto>(name8);
Console.WriteLine("");
Console.WriteLine(nameDto8.AllName);
Console.WriteLine();
//Console.ReadKey(); //9.属性格式化
Mapper.Reset();
Name name9 = new Name() { FirstName = "L", LastName = "jz" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.FirstName, opt => opt.AddFormatter<StringFormatter>());
NameDto nameDto9 = Mapper.Map<Name, NameDto>(name9);
Console.WriteLine("");
Console.WriteLine(nameDto9.FirstName);
Console.WriteLine();
//Console.ReadKey(); //10.属性null时的默认值
Mapper.Reset();
Name name10 = new Name() { FirstName = "L" };
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.LastName, opt => opt.NullSubstitute("jz"));
NameDto nameDto10 = Mapper.Map<Name, NameDto>(name10);
Console.WriteLine("");
Console.WriteLine(nameDto10.LastName);
Console.WriteLine();
//Console.ReadKey(); //其它设置与特性
//11.转换匿名对象
Mapper.Reset();
object name11 = new { FirstName = "L", LastName = "jz" };
NameDto nameDto11 = Mapper.DynamicMap<NameDto>(name11);
Console.WriteLine("");
Console.WriteLine(nameDto11.FirstName + nameDto11.LastName);
Console.WriteLine();
//Console.ReadKey(); //12.转换DataTable
Mapper.Reset();
DataTable dt = new DataTable();
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Rows.Add("L", "jz");
List<NameDto> nameDto12 = Mapper.DynamicMap<IDataReader, List<NameDto>>(dt.CreateDataReader());
Console.WriteLine("");
Console.WriteLine(nameDto12[].FirstName + nameDto12[].LastName);
Console.WriteLine();
//Console.ReadKey();
//emitMapper error
//List<NameDto> nameDto20 = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<IDataReader, List<NameDto>>().Map(dt.CreateDataReader()); //13.转化存在的对象
Mapper.Reset();
Mapper.CreateMap<Name, NameDto>()
.ForMember(name => name.LastName, opt => opt.Ignore());
Name name13 = new Name() { FirstName = "L" };
NameDto nameDto13 = new NameDto() { LastName = "jz" };
Mapper.Map<Name, NameDto>(name13, nameDto13);
//nameDto13 = Mapper.Map<Name, NameDto>(name13);//注意,必需使用上面的写法,不然nameDto13对象的LastName属性会被覆盖
Console.WriteLine("");
Console.WriteLine(nameDto13.FirstName + nameDto13.LastName);
Console.WriteLine();
//Console.ReadKey(); //14.Flatten特性
Mapper.Reset();
Mapper.CreateMap<Store, FlattenName>();
Store store2 = new Store() { Name = new Name() { FirstName = "L", LastName = "jz" } };
FlattenName nameDto14 = Mapper.Map<Store, FlattenName>(store2);
Console.WriteLine("");
Console.WriteLine(nameDto14.NameFirstname + nameDto14.NameLastName);
Console.WriteLine();
//Console.ReadKey(); //15.将Dictionary转化为对象,现在还不支持
Mapper.Reset();
Mapper.CreateMap<Dictionary<string, object>, Name>();
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("FirstName", "L");
//Name name15 = Mapper.DynamicMap<Dictionary<string, object>, Name>(dict);
Name name15 = Mapper.Map<Dictionary<string, object>, Name>(dict);
Console.WriteLine("");
Console.WriteLine(name15.FirstName);
Console.WriteLine();
Console.ReadKey();
}
} public class Store
{
public Name Name { get; set; }
public int Age { get; set; }
} public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
} public class StoreDto
{
public NameDto Name { get; set; }
public int Age { get; set; }
} public class NameDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string AllName { get; set; }
} public class FlattenName
{
public string NameFirstname { get; set; }
public string NameLastName { get; set; }
} public class NameConverter : TypeConverter<Name, NameDto>
{
protected override NameDto ConvertCore(Name source)
{
return new NameDto() { AllName = source.FirstName + source.LastName };
}
} public class NameResolver : ValueResolver<Name, NameDto>
{
protected override NameDto ResolveCore(Name source)
{
return new NameDto() { FirstName = source.FirstName, LastName = source.LastName };
}
} public class NameFormatter : ValueFormatter<NameDto>
{
protected override string FormatValueCore(NameDto name)
{
return name.FirstName + name.LastName;
}
} public class StringFormatter : ValueFormatter<string>
{
protected override string FormatValueCore(string name)
{
return name + "-";
}
}
}

AutoMapper实现对象转换的几种方式的更多相关文章

  1. C# 对象与JSON字符串互相转换的三种方式

    C# 对象与JSON字符串互相转换的三种方式 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换, ...

  2. JSON字符串互相转换的三种方式和性能比较

    C# 对象与JSON字符串互相转换的三种方式 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换, ...

  3. Java中对象拷贝的两种方式

    引用的拷贝 //引用拷贝 private static void copyReferenceObject(){ Person p = new Person(23, "zhang") ...

  4. Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式

    1.Class对象获取的三种方式 本文转载自:https://blog.csdn.net/hanchao5272/article/details/79361463 上一章节已经说过,一般情况下,Jav ...

  5. 检测对象类型的两种方式,constructor属性和instanceof

    //本例是为了记录检测对象类型的两种方式,即constructor属性和instanceof操作符.详见<高三>P145        function Person(name, age, ...

  6. Kubernetes 对象管理的三种方式

    Kubernetes 中文文档 1. Kubernetes 对象管理的三种方式对比 Kubernetes 中的对象管理方式,根据对象配置信息的位置不同可以分为两大类: 命令式:对象的参数通过命令指定 ...

  7. JS对象创建的几种方式整理

    ​ 本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...

  8. word HTML文件与Markdwon互相转换的几种方式

    Tip:word文件与Markdwon转换往往是可逆的.无论使用哪种方式,要想完美转换,必须要预先处理掉markdown与word不兼容的格式,如word文件对象,带边框的代码块等等 方法一:借助pa ...

  9. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

随机推荐

  1. idea 无法找到或加载主类

  2. C语言进阶之路(一)----C语言的内存四区模型

    内存四区模型:操作系统给C/C++编写的程序分配内存,通常将分配的内存划分为以下四个区域:1.栈区:存放局部变量,用完由操作系统自动释放2.堆区:动态分配给程序的内存区域,由程序员手动释放3.数据区: ...

  3. c++赋值构造函数为什么返回引用类型?

    目录 0 前言 1 内置类型 2 自定义类型 3 结论 4 源码 0. 前言 c++默认赋值构造函数的返回值是引用类型,c++赋值运算符=的本意是返回左值的引用,我们重载赋值构造函数的时候,返回值是否 ...

  4. Beta阶段敏捷冲刺日志的集合贴

    Beta阶段冲刺前计划与安排:http://www.cnblogs.com/software-teamwork/p/9060118.html Day1(5.22):https://www.cnblog ...

  5. sublime设置tab为四个空格

    在首选项->设置里: 添加: "tab_size": 4, "translate_tabs_to_spaces": true,

  6. 4A Watermelon

    A. Watermelon time limit per test 1 second memory limit per test 64 megabytes input standard input o ...

  7. zabbix 乱码问题

    一.乱码原因 查看cpu负载,中文乱码如下 这个问题是由于zabbix的web端没有中文字库,我们最需要把中文字库加上即可 二.解决zabbix乱码方法 2.1 上传字体文件到zabbix中 找到本地 ...

  8. Bitmap的秘密

    作者: 周海鹏  来源: infoQ  发布时间: 2015-02-13 11:26  阅读: 4999 次  推荐: 10   原文链接   [收藏] 之前已经参加过几次QCon峰会,不过今年QCo ...

  9. Codeforces 333E Summer Earnings - bitset

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定平面上的$n$个点,以三个不同点为圆心画圆,使得圆两两没有公共部分(相切不算),问最大的半径. 显然答案是三点间任意两点之间的距离的最小值 ...

  10. Java实现简单的RPC框架

    一.RPC简介 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用) ...