同一种类型的两个对象赋值,用反射。再也不用点属性了。。。。(适用于ef)
2015-08-21 15:24
645 查看
/// <summary> /// 给对象赋值的方法(不赋地址)(同一个类型),含过滤 /// </summary> /// <typeparam name="T"><peparam> /// <param name="left">=号左边</param> /// <param name="right">=号右边</param> /// <param name="id">过滤条件</param> public static void Assignment<T>(T left, T right, string id=null) { Type type = left.GetType(); List<PropertyInfo> pList = type.GetProperties().ToList(); for (int i = 0; i < pList.Count; i++) { //根据属性名获得指定的属性对象 PropertyInfo gc = type.GetProperty(pList[i].Name); //设置属性的值 if (id != null) { if (pList[i].Name != id) { gc.SetValue(left, pList[i].GetValue(right, null), null); } } else { gc.SetValue(left, pList[i].GetValue(right, null), null); } } }
View Code
相关文章推荐
- 常见的java 错误--转
- 【转载】Linux下SVN 检出windows SVN服务器上的项目出错:SSL handshake failed
- Linux常用命令
- 面试题目-1
- sepFilter2D函数
- [IOS]CoreAnimation基础[翻译3部分]
- swift 打印16进制数
- eclipse Kepler tomcat内存溢出解决方案
- IOS比较常用的第三方及实例(不断更新中)
- 电赛总结(三)——DA芯片总结
- "Could not load file or assembly \u0027Newtonsoft.Json, Version=4.5.0.0
- 内核注册流程
- Building OpenCV with Visual Studio 2015
- sizeof运算符
- POJ 3737 UmBasketella(三分模板)
- 常用正则表达式
- hive计算网页停留时长
- JS给元素增加className
- sql 自增序列
- DataTable RowFilter 过滤数据