同一种类型的两个对象赋值,用反射。再也不用点属性了。。。。(适用于ef)
2024-10-10 23:02:48
/// <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 = ; 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);
} }
}
最新文章
- LoadRunner函数百科叒叒叒更新了!
- C 语言中 setjmp 和 longjmp
- windows系统和ubuntu虚拟机之间文件共享——samba
- MVC文章汇总
- spring web mvc中遇到的错误以及学习小记(持续记录)
- php 读取文件的几种方法
- ActiveMQ简介与安装
- tahoma字体对中文字的影响
- 如何不保存Word即可用Word打开想显示的内容!
- [置顶] 学习VB.NET编程最基本的三个问题
- Asp.Net Core WebApi学习笔记(四)-- Middleware
- Flex TextInput 动态推断输入内容
- 规划(纪念我在ACM道路上的一年)
- (转)递归算法的时间复杂度终结篇与Master method
- Hello_World
- 最长公共前缀(python) leetcode答案
- git如何创建 .gitignore文件
- 从零搭建 ES 搜索服务(六)相关性排序优化
- python &; MySQLdb(one)
- 树莓派.Qt.Creator安装方法