对像相同赋值
来源:互联网 发布:如何下载英语软件 编辑:程序博客网 时间:2024/06/11 18:31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace BMW.Core.Common
{
public class ObjectUtils
{
public static List<string> CopyProperties(object src, object target)
{
return CopyProperties(src, target, null);
}
public static List<string> CopyProperties(object src, object target, string[] excludeProperties)
{
return CopyProperties(src, target, excludeProperties, true);
}
public static List<string> CopyProperties(object src, object target, string[] excludeProperties, bool ignoreCase)
{
List<string> copyList = new List<string>();
Type srcType=src.GetType();
Type tarType = target.GetType();
Dictionary<string, PropertyInfo> srcDic = new Dictionary<string, PropertyInfo>();
foreach (PropertyInfo info in srcType.GetProperties())
srcDic.Add(ignoreCase?info.Name.ToLower():info.Name,info);
if (excludeProperties != null)
{
foreach (string notKey in excludeProperties)
srcDic.Remove(ignoreCase ? notKey.ToLower() : notKey);
}
foreach (PropertyInfo tarInfo in tarType.GetProperties())
{
if (!tarInfo.CanWrite)
continue;
string key=ignoreCase?tarInfo.Name.ToLower():tarInfo.Name;
if (srcDic.ContainsKey(key))
{
PropertyInfo srcInfo = srcDic[key];
try
{
tarInfo.SetValue(target, srcInfo.GetValue(src, null), null);
}
catch (Exception ex)
{
throw new FormatException(string.Format("源对象的{0}属性类型为{1},不能成功转换为目标对象的{2}属性的{3}类型,请调整.",
srcInfo.Name, srcInfo.PropertyType.Name, tarInfo.Name, tarInfo.PropertyType),ex);
}
copyList.Add(tarInfo.Name);
}
}
return copyList;
}
}
}
- 对像相同赋值
- jquery对多个相同id的赋值
- 使用java 反射,对类中成员变量赋值.将Json对像转为Java对像
- 如果list中的对像的编号相同就把对像合并,并将数量相加
- 对不同对象的相同属性进行赋值,被改变的是des
- java中对象变量及new对像赋值的1个语法现像
- 相同实体字段的赋值
- R语言中像sas宏一样的对一行中每个值赋值给一个变量
- 对像序列化为 json 对像
- C++必知必会 - 赋值和初始化并不相同
- 相同属性的Bean相互赋值
- DozerBeanMapper对象之间相同属性名赋值
- javabean 不同的类 相同属性赋值
- 两个java对象相同属性赋值
- 给相同的类对象赋值
- 相同结构的两个类赋值
- 实体类相同属性之间相互赋值
- android实体类相同字段赋值
- 关于初始化块
- 编译原理之文法一
- 堆排(演示过程)
- Windows7下安装fedora16
- 明年的七夕,你还记得我吗
- 对像相同赋值
- 事业环境因素和组织过程资产
- java设计模式之享元模式Flyweight
- java技术支持 计算相差的天数
- 九大项炫酷科技,如何颠覆未来
- java设计模式之代理模式Proxy
- 3Q大战续集:腾讯再诉360索赔1.25亿
- WPF调用线程 解决“调用线程无法访问此对象,因为另一个线程拥有该对象“问题
- Android触屏滑动事件总结