博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AutoTransformHandler
阅读量:5063 次
发布时间:2019-06-12

本文共 4119 字,大约阅读时间需要 13 分钟。

public static ObservableCollection
Transform
(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; } } }

 

转载于:https://www.cnblogs.com/Wolfmanlq/p/4183419.html

你可能感兴趣的文章
OpenCV矩阵运算总结
查看>>
Java Build Practice 4:Extend and Invoke Ant API
查看>>
[转] Transformer图解
查看>>
FreeBSD方式安装 MAC OSX
查看>>
Linux 根文件系统制作
查看>>
IOS--沙盒机制
查看>>
使用 JointCode.Shuttle 访问任意 AppDomain 的服务
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>
【题解】[P4178 Tree]
查看>>
Jquery ui widget开发
查看>>
更改git仓库地址
查看>>
有标号DAG计数 [容斥原理 子集反演 组合数学 fft]
查看>>
Recipe 1.4. Reversing a String by Words or Characters
查看>>
Rule 1: Make Fewer HTTP Requests(Chapter 1 of High performance Web Sites)
查看>>
sql注入
查看>>
「破解」Xposed强
查看>>
src与href的区别
查看>>
ABAP工作区,内表,标题行的定义和区别
查看>>
《xxx重大需求征集系统的》可用性和可修改性战术分析
查看>>