标签:ati 实体 nat rop col int first nbsp color
这是一个封装的实体转换类
应用场景一张主表一张副表,副表为主表的备份表
//将源实体转换到空的实体类中 调用
主表实体类 entity = new 主表实体类();
EntityCoverter.Covert<主表实体类, 副表实体类>(entity);
//将源实体转换到非空的实体中 调用
主表实体类 entity = new 主表实体类();
副表实体类 hazardInfo = new 副表实体类();
EntityCoverter.Covert<主表实体类 , 副表实体类>(entity, hazardInfo);
/// <summary> /// 实体转换类 /// </summary> public class EntityCoverter { /// <summary> /// 将源实体转换到空的实体类中 /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="source"></param> /// <returns></returns> public static T2 Covert<T1, T2>(T1 source) { T2 result = default(T2); PropertyInfo[] pi = typeof(T2).GetProperties(); PropertyInfo[] pi1 = typeof(T1).GetProperties(); result = Activator.CreateInstance<T2>(); for (int i = 0; i < pi.Length; i++) { PropertyInfo temp = pi1.Where(t => t.Name == pi[i].Name).FirstOrDefault(); if (temp != null) { try { pi[i].SetValue(result, temp.GetValue(source, null), null); } catch (Exception ex) { } } } return result; } /// <summary> /// 将源实体转换到非空的实体中 /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public static T2 Covert<T1, T2>(T1 source, T2 destination) { PropertyInfo[] pi = typeof(T2).GetProperties(); PropertyInfo[] pi1 = typeof(T1).GetProperties(); for (int i = 0; i < pi.Length; i++) { PropertyInfo temp = pi1.Where(t => t.Name == pi[i].Name).FirstOrDefault(); if (temp != null) { try { pi[i].SetValue(destination, temp.GetValue(source, null), null); } catch { } } } return destination; } }
标签:ati 实体 nat rop col int first nbsp color
原文地址:https://www.cnblogs.com/macT/p/9205329.html