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. Android屏幕适配的一些常识

    屏幕适配的注意事项 1. AndroidManifest.xml设置 在中Menifest中添加子元素 android:anyDensity="true"时,应用程序安装在不同密度 ...

  2. mycat服务启动{管理模块启动过程}

    mycat启动的时候启动了三个模块 1:NIOConnector(负责链接mysql数据库,连接池以数据库为准不以链接字符串为准), 1:NIOAcceptor,ManagerConnectionFa ...

  3. Linux2.6 内核的 Initrd 机制解析(转)

    from: https://www.ibm.com/developerworks/cn/linux/l-k26initrd/ 简介: Linux 的 initrd 技术是一个非常普遍使用的机制,lin ...

  4. dwz_bootstrap + thinkphp

    http://www.thinkphp.cn/code/936.html  回去继续学习 SuperWebSocket

  5. vs多项目模板及add-in开发

    本文分2部分 第一为自定义多项目模板 第二为vs add-in开发 效果图 1.自定义模板 2. 工具菜单 3.窗口 4.工程 5.文件 ... 一. 多项目模板 单项目模板做起来很简单 选中一个项目 ...

  6. 使用https协议解决掉顽固不化的已解密的登录请求

    1.1 已解密的登录请求概述 在应用程序测试过程中,检测到将未加密的登录请求发送到服务器.由于登录过程所用的部分输入字段(例如:用户名.密码.电子邮件地址.社会保险号码,等等)是个人敏感信息,建议通过 ...

  7. EPLAN Electric P8 2.0即将到来,着实令人期待-转caodaping

    在今年的4月份,2.0版本的EPLAN Electric P8 首次揭开其神秘面纱,见诸于世.它的展露,再次印证了EPLAN 软件平台朝着"更实用"这一方向发展,同时也证明&quo ...

  8. 『WPF』实现拖动文件到窗体(控件)

    前言 实现从窗口外部拖文件到窗口内部并自动捕获文件地址. 第一步 开启属性 启用底层Window的AllowDrop属性,添加Drop事件. Drop事件:当你拖动文件到对应控件后,松开触发. 除Dr ...

  9. 在多线程环境中使用CoreData

    在多线程环境中使用CoreData BY 子非鱼 · 2014 年 10 月 13 日   上回书说道,其实CoreData学起来也没有很复杂,我们其实增删改查都和别的ORM大同小异.但是世界总是很复 ...

  10. iOS开发中一些常见的并行处理

    本文主要探讨一些常用多任务的最佳实践.包括Core Data的多线程访问,UI的并行绘制,异步网络请求以及一些在运行态内存吃紧的情况下处理大文件的方案等.??其实编写异步处理的程序有很多坑!所以,本文 ...