如何判断ArrayList,Hashtable,SortedList 这类对象是否相等 !

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8using System.Collections;
  9using System.IO;
 10using System.Xml.Serialization;
 11
 12namespace arrylistcheck
 13{
 14    public partial class Form1 : Form
 15    {
 16        public Form1()
 17        
 18            InitializeComponent();
 19        }

 20
 21
 22        private void button1_Click(object sender, EventArgs e)
 23        {
 24            ArrayList array1 = new ArrayList();
 25            ArrayList array2 = new ArrayList();
 26            ArrayList array3 = new ArrayList();
 27
 28            MyEntity entity1 = new MyEntity();
 29            entity1.Address = "peking";
 30            entity1.Job = "programmer";
 31            BaseInfo b1 = new BaseInfo();
 32            b1.Age = 10;
 33            b1.Name = "zzq";
 34            entity1.PersonalInfo = b1;
 35
 36            array1.Add(entity1);
 37            array2.Add(entity1);
 38            array3.Add(entity1);
 39            MyEntity entity2 = new MyEntity();
 40            entity2.Address = "Hebei";
 41            entity2.Job = "programmer";
 42            BaseInfo b2 = new BaseInfo();
 43            b2.Age = 20;
 44            b2.Name = "hongfang";
 45            entity2.PersonalInfo = b2;
 46
 47            array1.Add(entity2);
 48            array2.Add(entity2);
 49            array3.Add(entity2);
 50
 51            MyEntity entity3 = new MyEntity();
 52            entity3.Address = "unkown";
 53            entity3.Job = "programmer";
 54            BaseInfo b3 = new BaseInfo();
 55            b3.Age = 40;
 56            b3.Name = "zzq";
 57            entity3.PersonalInfo = b3;
 58
 59            array3.Add(entity3);
 60            Type[] tp =typeof(MyEntity), typeof(BaseInfo) };
 61            try
 62            {
 63                if (IsTwoObjectsEqual(array1, array2, typeof(ArrayList), tp))
 64                    MessageBox.Show("Array1 equals to array2");
 65                else
 66                    MessageBox.Show("Array1 does not equals to array2");
 67
 68                if (IsTwoObjectsEqual(array1, array3, typeof(ArrayList), tp))
 69                    MessageBox.Show("Array1 equals to array3");
 70                else
 71                    MessageBox.Show("Array1 does not equals to array3");
 72            }

 73            catch (Exception ex)
 74            {
 75                MessageBox.Show(ex.Message);
 76            }

 77
 78        }

 79        public static bool IsTwoObjectsEqual(object oOrigin, object oCompared, Type classType, Type[] includedType)
 80        {
 81            string strOrigin = string.Empty;
 82            string strCompared = string.Empty;
 83            FileStream fsOrigin = null;
 84            FileStream fsCompared = null;
 85            StreamReader objOriginReader = null;
 86            StreamReader objComparedReader = null;
 87
 88            try
 89            {
 90                XmlSerializer xs = new XmlSerializer(classType, includedType);
 91                if (File.Exists("d:\\Origin.txt")) File.Delete("d:\\Origin.txt");
 92                if (File.Exists("d:\\Compared.txt")) File.Delete("d:\\Compared.txt");
 93                fsOrigin = new FileStream("d:\\Origin.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
 94                xs.Serialize(fsOrigin, oOrigin);
 95                fsCompared = new FileStream("d:\\Compared.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
 96                xs.Serialize(fsCompared, oCompared);
 97
 98                //Read from the two files and compare .
 99                fsOrigin.Close();
100                fsCompared.Close();
101                objOriginReader = new StreamReader("d:\\Origin.txt");
102                objComparedReader = new StreamReader("d:\\Compared.txt");
103                strOrigin = objOriginReader.ReadToEnd();
104                strCompared = objComparedReader.ReadToEnd();
105                objOriginReader.Close();
106                objComparedReader.Close();
107                if (strOrigin == strCompared)
108                    return true;
109                else
110                    return false;
111            }

112            catch (Exception ex)
113            {
114                throw ex;
115            }

116            finally
117            {
118                fsOrigin.Close();
119                fsCompared.Close();
120                objOriginReader.Close();
121                objComparedReader.Close();
122            }

123        }

124    }

125  
126    public class MyEntity
127    {
128        public string Job;
129        public string Address;
130        public BaseInfo PersonalInfo;
131        public MyEntity()
132        {
133        }

134    }

135    public class BaseInfo
136    {
137        public string Name;
138        public int Age;
139
140    }

141}

昨天晚上发了n多次才成功,刚发现前面写的那些东西没有了。郁闷 !
串行化到任意流都是可以的,这个例子是用文件流了。用串行化的优势在于可以不考虑一个类的结构,或许类的属性本身就是对象,这样属性本身还会有属性,其他的方法似乎不能很好的解决。

只是给提供一种思路,如果大家有好的解决方案,联系我啊!zzq765#hotmail.com .
      

posted on 2005-12-27 22:33 zhanqiangz(闲云野鹤) 阅读(2146) 评论(5)  编辑 收藏

评论

#1楼  2005-12-28 00:02 双鱼座      

有很多的方法进行项的比较,比较系列化以后的值效率太低,不可推荐,即使系列化也应该系列化到一个内存流中而不是磁盘上。
以下方案可能会嫌笨,但却是平均效率最高的:
1.长度是否相等....第一次过滤;
2.项类型是否相同....第二次过滤;
3.执行两个比较项的每个Field的Equals....第三次过滤   回复  引用  查看    

#2楼  2005-12-28 09:15 仙桃人 [未注册用户]

override Equals method   回复  引用    

#3楼  2005-12-28 09:45 闭关|那一剑的风情      

重写MyEntity 的Equals方法,要简单很多

...........Equals(object obj)
{
if( obj == null)
return false;
if( obj.GetType() != this.GetType())
return false;

MyEntity other = obj as MyEntity;
if( this.Address != other.Address || this.Job != other.Job || !this.BaseInfo.Equals(obj.BaseInfo ))
return false;

return true;
}


BaseInfo


...........Equals(object obj)
{
if( obj == null)
return false;
if( obj.GetType() != this.GetType())
return false;

BaseInfoother = obj as BaseInfo;
if( this.Name!= other.Name|| this.Age!= other.Age)
return false;

return true;
}

  回复  引用  查看    

#4楼  2005-12-28 11:53 难得一蠢      

看看我的这个办法:

http://lixianhuei.cnblogs.com/archive/2005/12/28/306336.html   回复  引用  查看    

#5楼 [楼主] 2005-12-28 12:27 zhanqiangz(闲云野鹤)      

TO:双鱼座 偶原来写着用内存流了,但是发了多次才成功,前面的文字搞丢了。比较Field这个方法对于Filed本身是简单类型的的确很方便。

TO:闭关|那一剑的风情 如果对于哪一层属性是对象未知的类来说重写Equals也不是很方便,如果事先可以确定类的结构Equals方法应该是最好的。否则怕要用递归了。:)   回复  引用  查看    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-12-28 12:21 编辑过


相关链接:
 





<2005年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

如转载本博客的相关资料敬请注明出处.谢谢合作!

与我联系

搜索

 

常用链接

留言簿(6)

我参与的团队

我的标签

随笔分类(51)

随笔档案(49)

文章分类(7)

文章档案(5)

收藏夹(3)

.NET

BizTalk

Design

JavaScript

My Blogs

Power Tools

Process Control

Search Engine

SQL Server

XML

积分与排名

最新评论

阅读排行榜

评论排行榜