AutoTransformHandler
public static ObservableCollection<F> Transform<T, F>(List<T> target) where F : new() { ObservableCollection<F> source = new ObservableCollection<F>(); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform<T, F>(target[i]); source.Add(f); } return source; } public static List<F> Transform<T, F>(ObservableCollection<T> target) where F : new() { List<F> source = new List<F>(); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform<T, F>(target[i]); source.Add(f); } return source; } public static List<F> TransformList<T, F>(List<T> target) where F : new() { List<F> source = new List<F>(); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform<T, F>(target[i]); source.Add(f); } return source; } public static ObservableCollection<F> TransformObservableCollection<T, F>(ObservableCollection<T> target) where F : new() { ObservableCollection<F> source = new ObservableCollection<F>(); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform<T, F>(target[i]); source.Add(f); } return source; } public static F Transform<T, F>(T target) where F : new() { if (target == null) { return default(F); } F source = new F(); if (target != null) { PropertyInfo[] fromFields = null; PropertyInfo[] toFields = null; fromFields = typeof(T).GetProperties(); toFields = typeof(F).GetProperties(); SetProperties(fromFields, toFields, target, source); } return source; } public static void Transform<T, F>(T target, F source) { PropertyInfo[] fromFields = null; PropertyInfo[] toFields = null; fromFields = typeof(T).GetProperties(); toFields = typeof(F).GetProperties(); SetProperties(fromFields, toFields, target, source); } public static void SetProperties(PropertyInfo[] fromFields, PropertyInfo[] toFields, object fromRecord, object toRecord) { PropertyInfo fromField = null; PropertyInfo toField = null; if (fromFields == null) { return; } if (toFields == null) { return; } for (int f = 0; f < fromFields.Length; f++) { fromField = (PropertyInfo)fromFields[f]; for (int t = 0; t < toFields.Length; t++) { toField = (PropertyInfo)toFields[t]; if (fromField.Name.ToUpper() != toField.Name.ToUpper()) { continue; } if (toField.CanWrite) { if ((fromField.PropertyType == typeof(DateTime?) && toField.PropertyType == typeof(string))) { DateTime? fromDateTime = (DateTime?)fromField.GetValue(fromRecord, null); if (!fromDateTime.HasValue) { toField.SetValue(toRecord, null, null); } else { toField.SetValue(toRecord, fromDateTime.Value.ToString(), null); } break; } if (((fromField.PropertyType == typeof(DateTime) || fromField.PropertyType == typeof(Int32) || fromField.PropertyType == typeof(decimal?) ) && toField.PropertyType == typeof(string))) { object fromDateTime = fromField.GetValue(fromRecord, null); toField.SetValue(toRecord, fromDateTime.ToString(), null); break; } if ((fromField.PropertyType == typeof(DateTime?) && toField.PropertyType == typeof(long?))) { DateTime? fromDateTime = (DateTime?)fromField.GetValue(fromRecord, null); if (!fromDateTime.HasValue) { toField.SetValue(toRecord, null, null); } else { toField.SetValue(toRecord, (((DateTime)fromDateTime.Value).Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000, null); } break; } if (fromField.PropertyType == typeof(decimal?) && toField.PropertyType == typeof(double?)) { double? toDouble = null; if (fromField.GetValue(fromRecord, null) != null) { toDouble = double.Parse(fromField.GetValue(fromRecord, null).ToString()); } toField.SetValue(toRecord, toDouble, null); break; } if (fromField.PropertyType == typeof(bool?) && toField.PropertyType == typeof(string)) { string toString = null; if (fromField.GetValue(fromRecord, null) != null) { toString = fromField.GetValue(fromRecord, null).ToString(); } toField.SetValue(toRecord, toString, null); break; } toField.SetValue(toRecord, fromField.GetValue(fromRecord, null), null); break; } toField.SetValue(toRecord, fromField.GetValue(fromRecord, null), null); break; } } }
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力!
欢迎您持续关注我的博客:)
版权所有,欢迎保留原文链接进行转载:)