using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
People p = new People() { Age = 28, name = new Name() { FirstName = "zheng", LastName = "zhiqiang" } };
PeopleDto pDto = new PeopleDto();
pDto = AutoMapping<People, PeopleDto>.Convert(p, pDto);
Console.WriteLine(string.Format("{0} {1} {2}", pDto.Age, pDto.FirstName, pDto.LastName));
Console.Read();
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="T1">entity</typeparam>
/// <typeparam name="T2">dto</typeparam>
public static class AutoMapping<T1, T2>
where T1 : new()
where T2 : new()
{
public static T2 Convert(T1 t1, T2 t2)
{
var dtoPiList = t2.GetType().GetProperties().Where(x => x.PropertyType.IsPublic).ToList();
string name;
object value;
for (int i = 0; i < dtoPiList.Count(); i++)
{
name = dtoPiList[i].Name;
value = GetPropertyValue(t1, name);
if (null == value) continue;
dtoPiList[i].SetValue(t2, value, null);
}
return t2;
}
private static object GetPropertyValue(object t1, string name)
{
Type t = t1.GetType();
PropertyInfo[] pis = t.GetProperties();
//查找当前对象是否包含需要转换的属性
var piTemp = pis.Where(x => x.Name == name).FirstOrDefault();
//不存在递归查询
if (piTemp == null)
{
foreach (PropertyInfo pi in pis)
{
object obj1 = pi.GetValue(t1, null);
object obj2 = GetPropertyValue(obj1, name);
if (obj2 != null) { return obj2; }
}
}
else
{
//判断是否忽略标记
var cAttr = piTemp.GetCustomAttributes();
foreach (var attr in cAttr)
{
var tempAttr = attr as AutoMappingAttributes;
if (tempAttr.Ignore) { return null; }
}
//存在直接返回
return piTemp.GetValue(t1, null);
}
return null;
}
}
public class AutoMappingAttributes : Attribute
{
public bool Ignore { get; set; }
}
public class Name
{
public string FirstName { get; set; }
[AutoMappingAttributes(Ignore = true)]
public string LastName { get; set; }
} public class People
{
public int Age { get; set; }
public Name name { get; set; } }
public class NameDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class PeopleDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
} }

  

自己写的AutoMapper的更多相关文章

  1. 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)

    这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...

  2. Asp.net Core 2.2关于AutoMapper更初级的入门教程

    今天刚看到老张的哲学博客关于AutoMapper的教程,从壹开始前后端分离[ .NET Core2.0 +Vue2.0 ]框架之十三 || DTOs 对象映射使用,项目部署Windows+Linux完 ...

  3. 使用Adminlite + ASP.NET MVC5(C#) + Entityframework + AutoFac + AutoMapper写了个api接口文档管理系统

    一.演示: 接口查看:http://apidoc.docode.top/ 接口后台:http://apiadmin.docode.top/ 登录:administrator,123456 二.使用到的 ...

  4. 恋爱虽易,相处不易:当EntityFramework爱上AutoMapper

    剧情开始 为何相爱? 相处的问题? 女人的伟大? 剧情收尾? 有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易 ...

  5. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  6. 【AutoMapper官方文档】DTO与Domin Model相互转换(中)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  7. 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  8. 【道德经】漫谈实体、对象、DTO及AutoMapper的使用

    写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...

  9. AutoMapper(三)

    返回总目录 自定义类型转换 有时,需要完全控制一个类型到另一个类型的转换.一个类型一点都不像另一个类型,而且转换函数已经存在了,在这种情况下,你想要从一个“宽松”的类型转换成一个更强壮的类型,例如一个 ...

随机推荐

  1. [Unity] Shader - CG语言 流程控制语句

    CG语言中: 不支持 switch 语句(可以写,但不能很好的执行.) 循环语句中, 循环次数不能大于 1024 ,否则会报错. If...ELSE 条件判断语句: if (true) { } els ...

  2. 关于application/x-www-form-urlencoded等字符编码的解释说明

    在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...

  3. AndroidStudio导入Library

    1.把它像Module一样导入. File >New >ImportModule(选择你要导入的Library). 如果出现了下面的情况,意思是跟项目中的Module重名,改个名字就行了. ...

  4. 今天在学习NTP时发现了2个网站

    NTP 调整系统时间 一个网站是:http://chrony.tuxfamily.org/doc/1.31/manual.html  这个是专门介绍chrony的,做的很详细. 另外一个是:http: ...

  5. 在Heroku部署时,无法加载 css,js,图片资源解决办法

    解决方案: 首先查看Gemfile, 确保group :production do 里添加了 gem "rails_12factor", '0.0.2' 然后在本地执行 rails ...

  6. JavaScript类型判断instanceof与typeof对比

    经常有人会在JavaScript里写如下的方法: function checkType() { var s1 = 123; var s2 = "OK"; if (s1 instan ...

  7. PHP同时上传“多个”文件示例,并格式化$_FILES数组信息

    方法1: 在html表单,放置多个文件选择框, 使用数组名作为组件的名字,如下: <form action="upload.php" method="post&qu ...

  8. PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)

    1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...

  9. ahjesus Axure RP 7.0注册码

    ahjesus Axure RP 7.0注册码 用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+t ...

  10. linux source

    清华TUNA镜像源https://mirrors.tuna.tsinghua.edu.cn/ 中科大USTC镜像源 https://mirrors.ustc.edu.cn/ ali http://mi ...