public static ObservableCollectionTransform (List target) where F : new() { ObservableCollection source = new ObservableCollection (); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform (target[i]); source.Add(f); } return source; } public static List Transform (ObservableCollection target) where F : new() { List source = new List (); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform (target[i]); source.Add(f); } return source; } public static List TransformList (List target) where F : new() { List source = new List (); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform (target[i]); source.Add(f); } return source; } public static ObservableCollection TransformObservableCollection (ObservableCollection target) where F : new() { ObservableCollection source = new ObservableCollection (); for (int i = 0; i < target.Count; i++) { F f = new F(); f = Transform (target[i]); source.Add(f); } return source; } public static F Transform (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 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; } } }