如何判断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}
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 .