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;
}
}
}
如果您覺得本文對你有用,不妨幫忙點個贊,或者在評論裡給我一句贊美,小小成就都是今後繼續為大家編寫優質文章的動力!
歡迎您持續關注我的部落格:)
作者:Ken Wang
出處:http://www.cnblogs.com/Wolfmanlq/
版權所有,歡迎保留原文連結進行轉載:)