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; }
} }

  

最新文章

  1. 【转】JAVA自学之路
  2. August 15th 2016 Week 34th Monday
  3. 无锁数据结构(Lock-Free Data Structures)
  4. JAVA反射机制—学习总结
  5. [LeetCode]题解(python):098 Validate Binary Search Tree
  6. DZY Loves Chessboard
  7. jmeter 建立一个监控测试计划
  8. [REFERENCE] Real-Time-Normal-Map-Dxt-Compression
  9. 微信video标签全屏无法退出bug
  10. Heartbeat+DRBD+NFS 构建高可用的文件系统
  11. 通过ApplicationContextAwareSpring实现手工加载配置的javabean
  12. hdu1869六度分离(dijkstra)
  13. uVa 714 (二分法)
  14. CSS3-----图片翻页效果的展示
  15. URLWRITE视图重写技术
  16. Zim学习笔记 (Fedora)
  17. Jerry的ABAP, Java和JavaScript乱炖
  18. 解决Jenkins邮件配置问题
  19. golang go语言通道类型的通道示例 通道的通道
  20. 无法启动 nexus 服务,错误1067:进程意外终止。java环境变量设置技巧。

热门文章

  1. MySql类似Oracle的dual虚拟表
  2. javascript数据结构与算法---队列
  3. Shell入门教程:Shell当中的特殊变量
  4. mac 下修改jenkins的 端口号
  5. 【GoLang】golang 中 defer 参数的蹊跷
  6. JQuery 自动触发事件
  7. VIM插件攻略
  8. MySQL 5.5安装记录
  9. 使用 lsyncd 本地目录实时备份
  10. 解决JettyMavenPlugin: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;