using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Utils { /// /// 转换实体类 /// public class ConvertHelper { /// /// list实体转换 /// /// /// /// /// public static List ConvertToList(List source) { List t2List = new List(); if (source == null || source.Count == 0) return t2List; T2 model = default(T2); PropertyInfo[] pi = typeof(T2).GetProperties(); PropertyInfo[] pi1 = typeof(T1).GetProperties(); foreach (T1 t1Model in source) { model = Activator.CreateInstance(); foreach (var p in pi) { foreach (var p1 in pi1) { if (p.Name == p1.Name) { p.SetValue(model, p1.GetValue(t1Model, null), null); break; } } } t2List.Add(model); } return t2List; } /// /// entity实体转换 /// /// /// /// /// public static T2 ConvertToModel(T1 source) { T2 model = default(T2); PropertyInfo[] pi = typeof(T2).GetProperties(); PropertyInfo[] pi1 = typeof(T1).GetProperties(); model = Activator.CreateInstance(); foreach (var p in pi) { foreach (var p1 in pi1) { if (p.Name == p1.Name) { p.SetValue(model, p1.GetValue(source, null), null); break; } } } return model; } } }