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的更多相关文章
随机推荐
- android如何播放和录制音频
视频录制功能正在走来,在Androidsdk中有与之相关的类:android.media.MediaRecorder.当然,因为模拟器上没有提供必要的硬件设施,所以在学习过程中并不能实现.Media能 ...
- Oracle中的索引详解
Oracle中的索引概述 索引与表一样,也属于段(segment)的一种.里面存放了用户的数据,跟表一样需要占用磁盘空间.索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是 ...
- Android 初始化Setup Wizard——Provision
今天说说Provision这个APK,可能很多朋友都不知道有这个APK存在.Provision的作用很简单,就是一个系统初始化引导程序,原生的Android里面Provision只做了一件事,就是写入 ...
- SqlServer数据库备份与还原
http://v.youku.com/v_show/id_XMjA4NzcyNzUy.html http://v.youku.com/v_show/id_XMjA4Nzc0NDQw.html
- 2013年ACM湖南省赛总结
今年的比赛最大的变化就是改用OJ判题了,相比于PC^2确实省事了不少,至少可以直接复制样例了.题目方面依旧是刘汝佳命题,这点还是相当好的,至少给人以足够的安全感. 开始比赛之后安叔瞬间就把前半部分题目 ...
- android studio svn不显示问题
今天更新android studio SDK 重启后发现SVN功能没有了,工具栏也没有了更新提交按钮,刚开始以后是SVN程序可以不行了(在我的电脑提交更新没问题)我在官网下了最新1.9.4 打开and ...
- 39. Volume Rendering Techniques
Milan Ikits University of Utah Joe Kniss University of Utah Aaron Lefohn University of California, D ...
- leveldb - 并发写入处理
在并发写入的时候,leveldb巧妙地利用一个时间窗口做batch写入,这部分代码值得一读: Status DBImpl::Write(const WriteOptions& options, ...
- End of HTML blink tag
Recently I have read a news which said "Firfox 23 nixes support for outdated blink HTML tag&quo ...
- 阿里云 Redis 服务遇到的问题
ERR unknown command eval 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: St ...