C# 方便的复制/比较物件内数据的方法(Object Copy / Compare)
2011-06-28 09:20
549 查看
用 Collection / Object 时经常想用一个物件(Object)去生成另一个物件并保留原有数据(如 DataTable.Copy()),最没头没脑的做法是:
view plaincopy to clipboardprint?
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
oNewObject.Value1 = this.Value1;
oNewObject.Value2 = this.Value2;
oNewObject.Value3 = this.Value3;
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
return (oNewObject.Value1 == this.Value1
&& oNewObject.Value2 == this.Value2
&& oNewObject.Value3 == this.Value3);
}
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
oNewObject.Value1 = this.Value1;
oNewObject.Value2 = this.Value2;
oNewObject.Value3 = this.Value3;
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
return (oNewObject.Value1 == this.Value1
&& oNewObject.Value2 == this.Value2
&& oNewObject.Value3 == this.Value3);
}
如果每个物件(Object )只有十多个属性(Properties)的话这样只是小菜一碟,但当你有数十个物件...每个物件有数十个属性的话,那就有够脑残了...
以下是用Reflection的PropertyInfo去提取物件属性的例子:
view plaincopy to clipboardprint?
using System.Reflection;
.
.
.
for(int i = 0; i < oOldObject.GetType().GetProperties().Length; i ++)
{
//根据索引提取个别属性
PropertyInfo oOldObjectProperty = (PropertyInfo) oOldObject.GetType().GetProperties().GetValue(i);
//得到该属性的数据
Object oOldObjectValue = oOldObjectProperty.GetValue(oOldObject, null);
if (oOldObjectProperty.CanRead)
{
Console.WriteLine(oOldObjectProperty.Name + " = " + oOldObjectValue.ToString());
}
}
using System.Reflection;
.
.
.
for(int i = 0; i < oOldObject.GetType().GetProperties().Length; i ++)
{
//根据索引提取个别属性
PropertyInfo oOldObjectProperty = (PropertyInfo) oOldObject.GetType().GetProperties().GetValue(i);
//得到该属性的数据
Object oOldObjectValue = oOldObjectProperty.GetValue(oOldObject, null);
if (oOldObjectProperty.CanRead)
{
Console.WriteLine(oOldObjectProperty.Name + " = " + oOldObjectValue.ToString());
}
}
如此便能列出物件中的所有属性,用同样的方法应该就能轻松复制/比较两个物件了:
view plaincopy to clipboardprint?
using System.Reflection;
.
.
.
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
for(int i = 0; i < this.GetType().GetProperties().Length; i ++)
{
PropertyInfo oOldObjectProperty = (PropertyInfo) this.GetType().GetProperties().GetValue(i);
Object oOldObjectValue = oOldObjectProperty.GetValue(this, null);
PropertyInfo oNewObjectProperty = (PropertyInfo) oNewObject.GetType().GetProperties().GetValue(i);
if (oOldObjectProperty.CanRead)
{
//设定oNewObject的当前属性的值为oOldObjectValue(数据类型必须(必定)一致)
oNewObjectProperty.SetValue(oNewObject, oOldObjectValue, null);
}
}
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
bool bResult = true;
for(int i = 0; i < this.GetType().GetProperties().Length; i ++)
{
PropertyInfo oOldObjectProperty = (PropertyInfo) this.GetType().GetProperties().GetValue(i);
Object oOldObjectValue = oOldObjectProperty.GetValue(this, null);
PropertyInfo oNewObjectProperty = (PropertyInfo) oNewObject.GetType().GetProperties().GetValue(i);
Object oNewObjectValue = oNewObjectProperty.GetValue(oNewObject, null);
//防止属性是NULL
if (oOldObjectValue != null && oNewObjectValue != null)
{
switch(oOldObjectProperty.PropertyType.ToString())
{
case "System.Double":
//Double 会有机会变成如2.49999999...的无限小数, 变成String才准确
bResult = (((double)oOldObjectValue).ToString("0.000000") == ((double)oNewObjectValue).ToString("0.000000"));
break;
case "System.DateTime":
//量度到秒就够了
bResult = (((DateTime)oOldObjectValue).ToString("yyyy/MM/dd HH:mm:ss") == ((DateTime)oNewObjectValue).ToString("yyyy/MM/dd HH:mm:ss"));
break;
case "System.Boolean":
bResult = (((bool)oOldObjectValue) == ((bool)oNewObjectValue));
break;
default:
bResult = (oOldObjectValue.ToString() == oNewObjectValue.ToString());
break;
}
}
else
{
//验证个别属性是NULL
if (!(oOldObjectValue == null && oOldObjectValue == null))
{
bResult = false;
}
}
//如发现有不同就不用再比对了
if (!bResult)
{
break;
}
}
return bResult;
}
view plaincopy to clipboardprint?
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
oNewObject.Value1 = this.Value1;
oNewObject.Value2 = this.Value2;
oNewObject.Value3 = this.Value3;
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
return (oNewObject.Value1 == this.Value1
&& oNewObject.Value2 == this.Value2
&& oNewObject.Value3 == this.Value3);
}
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
oNewObject.Value1 = this.Value1;
oNewObject.Value2 = this.Value2;
oNewObject.Value3 = this.Value3;
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
return (oNewObject.Value1 == this.Value1
&& oNewObject.Value2 == this.Value2
&& oNewObject.Value3 == this.Value3);
}
如果每个物件(Object )只有十多个属性(Properties)的话这样只是小菜一碟,但当你有数十个物件...每个物件有数十个属性的话,那就有够脑残了...
以下是用Reflection的PropertyInfo去提取物件属性的例子:
view plaincopy to clipboardprint?
using System.Reflection;
.
.
.
for(int i = 0; i < oOldObject.GetType().GetProperties().Length; i ++)
{
//根据索引提取个别属性
PropertyInfo oOldObjectProperty = (PropertyInfo) oOldObject.GetType().GetProperties().GetValue(i);
//得到该属性的数据
Object oOldObjectValue = oOldObjectProperty.GetValue(oOldObject, null);
if (oOldObjectProperty.CanRead)
{
Console.WriteLine(oOldObjectProperty.Name + " = " + oOldObjectValue.ToString());
}
}
using System.Reflection;
.
.
.
for(int i = 0; i < oOldObject.GetType().GetProperties().Length; i ++)
{
//根据索引提取个别属性
PropertyInfo oOldObjectProperty = (PropertyInfo) oOldObject.GetType().GetProperties().GetValue(i);
//得到该属性的数据
Object oOldObjectValue = oOldObjectProperty.GetValue(oOldObject, null);
if (oOldObjectProperty.CanRead)
{
Console.WriteLine(oOldObjectProperty.Name + " = " + oOldObjectValue.ToString());
}
}
如此便能列出物件中的所有属性,用同样的方法应该就能轻松复制/比较两个物件了:
view plaincopy to clipboardprint?
using System.Reflection;
.
.
.
public MyObject Copy()
{
MyObject oNewObject = new MyObject();
for(int i = 0; i < this.GetType().GetProperties().Length; i ++)
{
PropertyInfo oOldObjectProperty = (PropertyInfo) this.GetType().GetProperties().GetValue(i);
Object oOldObjectValue = oOldObjectProperty.GetValue(this, null);
PropertyInfo oNewObjectProperty = (PropertyInfo) oNewObject.GetType().GetProperties().GetValue(i);
if (oOldObjectProperty.CanRead)
{
//设定oNewObject的当前属性的值为oOldObjectValue(数据类型必须(必定)一致)
oNewObjectProperty.SetValue(oNewObject, oOldObjectValue, null);
}
}
return oNewObject;
}
public bool Compare(MyObject oNewObject)
{
bool bResult = true;
for(int i = 0; i < this.GetType().GetProperties().Length; i ++)
{
PropertyInfo oOldObjectProperty = (PropertyInfo) this.GetType().GetProperties().GetValue(i);
Object oOldObjectValue = oOldObjectProperty.GetValue(this, null);
PropertyInfo oNewObjectProperty = (PropertyInfo) oNewObject.GetType().GetProperties().GetValue(i);
Object oNewObjectValue = oNewObjectProperty.GetValue(oNewObject, null);
//防止属性是NULL
if (oOldObjectValue != null && oNewObjectValue != null)
{
switch(oOldObjectProperty.PropertyType.ToString())
{
case "System.Double":
//Double 会有机会变成如2.49999999...的无限小数, 变成String才准确
bResult = (((double)oOldObjectValue).ToString("0.000000") == ((double)oNewObjectValue).ToString("0.000000"));
break;
case "System.DateTime":
//量度到秒就够了
bResult = (((DateTime)oOldObjectValue).ToString("yyyy/MM/dd HH:mm:ss") == ((DateTime)oNewObjectValue).ToString("yyyy/MM/dd HH:mm:ss"));
break;
case "System.Boolean":
bResult = (((bool)oOldObjectValue) == ((bool)oNewObjectValue));
break;
default:
bResult = (oOldObjectValue.ToString() == oNewObjectValue.ToString());
break;
}
}
else
{
//验证个别属性是NULL
if (!(oOldObjectValue == null && oOldObjectValue == null))
{
bResult = false;
}
}
//如发现有不同就不用再比对了
if (!bResult)
{
break;
}
}
return bResult;
}
相关文章推荐
- C# 方便的复制/比较物件内数据的方法(Object Copy / Compare)
- ArcEngine,C#删除数据几种方法和性能比较
- 比较C#中几种常见的复制字节数组方法的效率
- C# 使用 SqlBulkCopy 类批量复制数据到数据库
- C# 使用 SqlBulkCopy 类批量复制数据到数据库
- ArcEngine,C#数据删除几种方法以及性能比较
- C# 表复制和数据行的复制说明(Clone、ImportRow 、Copy )
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
- C# 表复制和数据行的复制说明(Clone、ImportRow 、Copy )
- 写一个方法clone; 实现js五种数据类型(string, number, boolean, array, object)的复制
- C#实现xml文件反序列化读入数据到object的方法
- 如何使用户能像Excel一样方便的复制Datagridview中的单元格区域数据[C#]
- 使用SqlBulkCopy对象进行大容量复制数据出现Datardader已经存在的错误的解决方法
- 比较C#中几种常见的复制字节数组方法的效率[转]
- C# 时间比较方法DateTime.Compare
- C#比较两个由基本数据类型构成的object类型
- C#部分---arraylist集合、arraylist集合中的object数据转换成int类string类等;间隔时间的表示方法;
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法) .
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法