BindObject
1public class BindObject
2 {
3 绑定#region 绑定
4
5 public static object[] BindObjectToArray(Type instanceType, IDataReader reader)
6 {
7 try
8 {
9 PropertyInfo [] properties = instanceType.GetProperties();
10 ArrayList items = new ArrayList();
11 Hashtable fieldName = new Hashtable();
12 int fieldCount = reader.FieldCount;
13 for (int i = 0; i < fieldCount; i++)
14 {
15 string name = reader.GetName(i).ToString().ToLower();
16 fieldName.Add(name, i);
17 }
18 while (reader.Read())
19 {
20 object instance = Activator.CreateInstance(instanceType);
21 foreach (PropertyInfo property in properties)
22 {
23 object[] attributes = property.GetCustomAttributes(typeof(MappingFieldAttribute), false);
24 object[] encoders = property.GetCustomAttributes(typeof(CharacterEncodeAttribute), false);
25 string aName = string.Empty;
26 CharacterEncodeAttribute encoder = null;
27 if(attributes != null && attributes.Length > 0)
28 {
29 MappingFieldAttribute mapping = attributes[0] as MappingFieldAttribute;
30 aName = mapping.FieldName.ToLower();
31 }
32 if (encoders != null && encoders.Length > 0)
33 {
34 encoder = encoders[0] as CharacterEncodeAttribute;
35 }
36 string pName = property.Name.Trim().ToLower();
37 int index = -1;
38 if (fieldName.ContainsKey(pName))
39 index = (int)fieldName[pName];
40 else if (aName != string.Empty && fieldName.ContainsKey(aName))
41 index = (int)fieldName[aName];
42 if (index >= 0)
43 {
44 object obj = reader[index];
45 Type propertyType = property.PropertyType;
46 if (propertyType.IsArray || propertyType.IsInterface)
47 {
48 continue;
49 }
50 else if (property.PropertyType.IsEnum)
51 {
52 if(obj != null && obj != DBNull.Value)
53 property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
54 }
55 else
56 {
57 if(obj != null && obj != DBNull.Value)
58 {
59 if(property.PropertyType == typeof(string) && encoder != null)
60 property.SetValue(instance, encoder.Encode(obj.ToString()), null);
61 else
62 property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
63 }
67 }
68 }
69 }
70 items.Add(instance);
71 }
72 if (!reader.IsClosed)
73 reader.Close();
74 object [] instances = (object[])Array.CreateInstance(instanceType, items.Count);
75 for(int i = 0; i < items.Count; i++)
76 {
77 //string str= "<![CDATA[" + items[i].ToString() +"]]>";
78 instances[i] = items[i];
79 }
80 return instances;
81 }
82 catch(Exception ex)
83 {
84 throw ex;
85 }
86 }
87
88
89 /**//// <summary>
90 /// 绑定
91 /// </summary>
92 /// <param name="instanceType"></param>
93 /// <param name="reader"></param>
94 /// <returns></returns>
95 public static IList BindObjectToInstance(Type instanceType, IDataReader reader)
96 {
97 PropertyInfo [] properties = instanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
98 ArrayList items = new ArrayList();
99 Hashtable fieldName = new Hashtable();
100 int fieldCount = reader.FieldCount;
101 for (int i = 0; i < fieldCount; i++)
102 {
103 string name = reader.GetName(i).ToString().ToLower();
104 fieldName.Add(name, i);
105 }
106 while (reader.Read())
107 {
108 object instance = Activator.CreateInstance(instanceType);
109 foreach (PropertyInfo property in properties)
110 {
111 string pName = property.Name.Trim().ToLower();
112 int index = -1;
113 if (fieldName.ContainsKey(pName))
114 index = (int)fieldName[pName];
115 if (index >= 0)
116 {
117 object obj = reader[index];
118 Type propertyType = property.PropertyType;
119 if (propertyType.IsArray || propertyType.IsInterface)
120 {
121 continue;
122 }
123 else if (property.PropertyType.IsEnum)
124 {
125 if(obj != null && obj != DBNull.Value)
126 property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
127 }
128 else
129 {
130 if(obj != null && obj != DBNull.Value)
131 property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
132 }
133 }
134 }
135 items.Add(instance);
136 }
137 if (!reader.IsClosed)
138 reader.Close();
139 return items;
140 }
141 #endregion
142 }
1public class BindObject
2 {
3 绑定#region 绑定
4
5 public static object[] BindObjectToArray(Type instanceType, IDataReader reader)
6 {
7 try
8 {
9 PropertyInfo [] properties = instanceType.GetProperties();
10 ArrayList items = new ArrayList();
11 Hashtable fieldName = new Hashtable();
12 int fieldCount = reader.FieldCount;
13 for (int i = 0; i < fieldCount; i++)
14 {
15 string name = reader.GetName(i).ToString().ToLower();
16 fieldName.Add(name, i);
17 }
18 while (reader.Read())
19 {
20 object instance = Activator.CreateInstance(instanceType);
21 foreach (PropertyInfo property in properties)
22 {
23 object[] attributes = property.GetCustomAttributes(typeof(MappingFieldAttribute), false);
24 object[] encoders = property.GetCustomAttributes(typeof(CharacterEncodeAttribute), false);
25 string aName = string.Empty;
26 CharacterEncodeAttribute encoder = null;
27 if(attributes != null && attributes.Length > 0)
28 {
29 MappingFieldAttribute mapping = attributes[0] as MappingFieldAttribute;
30 aName = mapping.FieldName.ToLower();
31 }
32 if (encoders != null && encoders.Length > 0)
33 {
34 encoder = encoders[0] as CharacterEncodeAttribute;
35 }
36 string pName = property.Name.Trim().ToLower();
37 int index = -1;
38 if (fieldName.ContainsKey(pName))
39 index = (int)fieldName[pName];
40 else if (aName != string.Empty && fieldName.ContainsKey(aName))
41 index = (int)fieldName[aName];
42 if (index >= 0)
43 {
44 object obj = reader[index];
45 Type propertyType = property.PropertyType;
46 if (propertyType.IsArray || propertyType.IsInterface)
47 {
48 continue;
49 }
50 else if (property.PropertyType.IsEnum)
51 {
52 if(obj != null && obj != DBNull.Value)
53 property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
54 }
55 else
56 {
57 if(obj != null && obj != DBNull.Value)
58 {
59 if(property.PropertyType == typeof(string) && encoder != null)
60 property.SetValue(instance, encoder.Encode(obj.ToString()), null);
61 else
62 property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
63 }
67 }
68 }
69 }
70 items.Add(instance);
71 }
72 if (!reader.IsClosed)
73 reader.Close();
74 object [] instances = (object[])Array.CreateInstance(instanceType, items.Count);
75 for(int i = 0; i < items.Count; i++)
76 {
77 //string str= "<![CDATA[" + items[i].ToString() +"]]>";
78 instances[i] = items[i];
79 }
80 return instances;
81 }
82 catch(Exception ex)
83 {
84 throw ex;
85 }
86 }
87
88
89 /**//// <summary>
90 /// 绑定
91 /// </summary>
92 /// <param name="instanceType"></param>
93 /// <param name="reader"></param>
94 /// <returns></returns>
95 public static IList BindObjectToInstance(Type instanceType, IDataReader reader)
96 {
97 PropertyInfo [] properties = instanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
98 ArrayList items = new ArrayList();
99 Hashtable fieldName = new Hashtable();
100 int fieldCount = reader.FieldCount;
101 for (int i = 0; i < fieldCount; i++)
102 {
103 string name = reader.GetName(i).ToString().ToLower();
104 fieldName.Add(name, i);
105 }
106 while (reader.Read())
107 {
108 object instance = Activator.CreateInstance(instanceType);
109 foreach (PropertyInfo property in properties)
110 {
111 string pName = property.Name.Trim().ToLower();
112 int index = -1;
113 if (fieldName.ContainsKey(pName))
114 index = (int)fieldName[pName];
115 if (index >= 0)
116 {
117 object obj = reader[index];
118 Type propertyType = property.PropertyType;
119 if (propertyType.IsArray || propertyType.IsInterface)
120 {
121 continue;
122 }
123 else if (property.PropertyType.IsEnum)
124 {
125 if(obj != null && obj != DBNull.Value)
126 property.SetValue(instance, Enum.Parse(property.PropertyType, obj.ToString(), true), null);
127 }
128 else
129 {
130 if(obj != null && obj != DBNull.Value)
131 property.SetValue(instance, Convert.ChangeType(obj, property.PropertyType), null);
132 }
133 }
134 }
135 items.Add(instance);
136 }
137 if (!reader.IsClosed)
138 reader.Close();
139 return items;
140 }
141 #endregion
142 }
下面再给出一个结合两者使用的方法:
Test(int id)
1 public IList Test(int id)
2 {
3 using (DataHelper helper = new DataHelper("server=localhost;database=pubs;uid=sa;pwd=111;"))
4 {
5 IDataReader reader = helper.CreateQuery(@"Select * From Table1 Where id=:id")
6 .SetInt32("id", id)
7 .ExecuteReader();
8 return BindObject.BindObjectToInstance(typeof(Table1), reader);
9 }
10 }
1 public IList Test(int id)
2 {
3 using (DataHelper helper = new DataHelper("server=localhost;database=pubs;uid=sa;pwd=111;"))
4 {
5 IDataReader reader = helper.CreateQuery(@"Select * From Table1 Where id=:id")
6 .SetInt32("id", id)
7 .ExecuteReader();
8 return BindObject.BindObjectToInstance(typeof(Table1), reader);
9 }
10 }
这个操作DataHelper的方法比较简单一看就明白我这里就不详细介绍了,简单的介绍下BindObject类,它是运用了反射的方式来对实体中的字段进行读取比较,然后赋值的,所以实体中的字段必须对应我们所写sql中的列。
个人觉得这些都没啥技术含量,其实真正重要是思想,是一种解决问题的方法。
注:这里的BindObject类里还涉及到自定义属性的问题,直接拷贝进去是运行不了,在下篇里将继续附上该类代码。并介绍其用处。呵呵。。。
本人水平有限,希望能迎来大家的“砖头”,越多越好咯。。。