public static ObservableCollection<F> Transform<T, F>(List<T> target)
where F : new()
{
ObservableCollection<F> source = new ObservableCollection<F>();
for (int i = ; 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 = ; 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 = ; 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 = ; 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 = ; f < fromFields.Length; f++)
{
fromField = (PropertyInfo)fromFields[f]; for (int t = ; 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(, , ).Ticks) / ,
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; }
}
}

AutoTransformHandler的更多相关文章

随机推荐

  1. mybatis热加载的实现

    最近在使用mybatis,由于是刚刚开始用,用的并不顺手,目前是感觉有2个地方非常的不好用: 1.mybatis调试不方便 由于dao层只有接口,实现只是一个map的xml文件,想加断点都没有地方加, ...

  2. 利用Mahout实现在Hadoop上运行K-Means算法

    利用Mahout实现在Hadoop上运行K-Means算法 一.介绍Mahout Mahout是Apache下的开源机器学习软件包,目前实现的机器学习算法主要包含有协同过滤/推荐引擎,聚类和分类三个部 ...

  3. count有关

    1.count有两个作用:统计某个字段有值的记录数:统计结果集的记录数.2.count括号内的表达式不为null,就是统计结果集的记录数.也就是说,count(1),count(*),count(10 ...

  4. <a>标签href属性与onclick事件

    a标签主要用来实现页面跳转,可以通过href属性实现,也可以在onclick事件里实现. <a onclick="window.location.href='www.cnblogs.c ...

  5. 用git写书

    apebook.org  www.apebook.org 最好的程序员图书免费托管服务 apebook 提供了 gitbook.com 类似的云端图书托管能力,图书基于广受欢迎的 gitbook 工具 ...

  6. Javascript - 栈 和 单链表

    最近在重温数据结构,于是写了一些代码玩玩,都是很初级的,表喷各位.... function Stack() { this.dataStore = []; this.top = 0; } Stack.p ...

  7. SqlServer查看各个表所占空间大小的sql

    CREATE TABLE [dbo].#tableinfo( 表名 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL, 记录数 [int] NULL, 预留空间 ...

  8. Rails: No such file or directory - getcwd

    这个的意思就是你从一个删除的目录里面运行实例:rails s

  9. Winform中进行MD5加密

    Winform,c#进行MD5加密直接上步骤: 1.添加引用 2.在.NET选项卡中找到“System.Web” 3.选中之后,点击“确定”即可,此时会在解决方案中看到刚才添加的引用 4.引用名空间: ...

  10. iis 访问网站需要进行身份验证

    今天网站输入域名访问的时候提示需要输入账号密码,这是权限出了问题,百度了一下,解决了,分享一下: 1.登陆远程,右键我的电脑->管理->本地用户和组->用户,里面有一个IUSR_WD ...