AutoTransformHandler
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的更多相关文章
随机推荐
- 无线局域网络 WIFI/WAPI/WLAN区别浅析
WIFI和WAPI的区别 既然WIFI和WAPI都是WLAN的传输协议,那么两者究竟都有怎样的区别? 首先第一点区别在于,两者的缔造者不一样.WIFI是又国外制定的一个协议,而WAPI是由中国制定的, ...
- 江豚科技|专业移动APP开发与移动互联网解决方案
北京江豚科技(www.eoiiioe.com)是国内领先的移动APP开发解决方案服务商,总部在中国的硅谷--中关村,分别在郑州.深圳设有服务机构. 江豚科技承接各类移动app开发外包和软件定制开发,我 ...
- tomcat7需要进行升级,因为有漏洞,而且安装包没有做过优化处理
http://www.open-open.com/lib/view/open1401931407228.html http://www.cnblogs.com/ggjucheng/archive/20 ...
- 关于ScrollerView的一些小心得
在项目开发时遇到一个问题,我在UIViewController上面直接创建了一个UIScrollerView,把UIScrollerView作为一个子视图添加到了UIViewController, 又 ...
- TStringList中AddObject使用
结构体定义 PYpType=^TYpType; TYpType=record yfcode:string; ypcode:string; YpUnitPrice:Currency; ...
- 利用Mysql提供的字符串方法查找字符串中某字符出现的次数
有这么一个需求,查出分类中没有子分类的一级分类,脑海中首次出现的解决思路和这样的 先使用PHP查出所有的一级分类 递归查询一级分类是否有子分类 将没有子分类的一级分类汇总 但觉的这样处理太麻烦了,然后 ...
- 前端开发者进阶之函数反柯里化unCurrying
函数柯里化,是固定部分参数,返回一个接受剩余参数的函数,也称为部分计算函数,目的是为了缩小适用范围,创建一个针对性更强的函数. 那么反柯里化函数,从字面讲,意义和用法跟函数柯里化相比正好相反,扩大适用 ...
- Android学习之AsyncTask
我们在<Android学习之Handler消息传递机制>(http://www.cnblogs.com/zhouhb/p/5812447.html)已提到过,Android只允许UI线程修 ...
- ffmpeg安装的问题
php语音转换需要安装ffmpeg文件 参考地址: http://thierry-xing.iteye.com/blog/2017864 http://diogomelo.net/blog/11/en ...
- 我也要学C语言-第十九章:命令行参数
C语言的语法规定main函数是带连个参数的,因为当初是考虑是在控制台下写程序.于是用户可以给参数微控程序.其实现在的WINDOWS程序也可以带参数.一般正规军写的应该程序一般都带命令行参数,帮助文档, ...