1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
using NickLee.Common;
12
13
/**//// <summary>
14
/// cXTM_User.序列化
15
/// </summary>
16
[Serializable]
17
public class cXTM_User : NickLee.Common.IDomain
18

{
19
private int _dID;
20
private string _userName;
21
public cXTM_User()
22
{
23
//
24
// TODO: 在此处添加构造函数逻辑
25
//
26
}
27
28
public int DID
29
{
30
get
31
{
32
return _dID;
33
}
34
set
35
{
36
_dID = value;
37
}
38
}
39
40
public string UserName
41
{
42
get
43
{
44
return _userName;
45
}
46
set
47
{
48
_userName = value;
49
}
50
}
51
52
/**//// <summary>
53
/// 属性列表数组
54
/// Propertylist中数组请定义该类中属性,并确保名称唯一性
55
/// 以及与XTM_User中SelectXTM_UserByKey_Test中查询名称统一
56
/// </summary>
57
public string[] Propertylist
58
{
59
get
60
{
61
return new string[]
{ "UserName", "DID" };
62
}
63
}
64
}
65

2

3

4

5

6

7

8

9

10

11

12

13


14

15

16

17

18



19

20

21

22



23

24

25

26

27

28

29



30

31



32

33

34

35



36

37

38

39

40

41



42

43



44

45

46

47



48

49

50

51

52


53

54

55

56

57

58



59

60



61



62

63

64

65

Object/DataSet Relational Mapping(对象/数据集关系映射)已经完成封装,现公布所有封装代码
ODRM.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Data;
5
using System.Collections;
6
using System.Reflection;
7
8
namespace NickLee.Common
9

{
10
public class ODRM
11
{
12
通用函数#region 通用函数
13
14
/**//// <summary>
15
/// 转换为DataTable
16
/// </summary>
17
/// <param name="Source">数据源</param>
18
/// <param name="DataMember">数据表名称</param>
19
public static DataTable ConvertDataTable(object Source, string DataMember)
20
{
21
DataTable baseTable = new DataTable();
22
if (Source is DataTable)
23
{
24
baseTable = (DataTable)Source;
25
return baseTable;
26
}
27
if (Source is DataSet)
28
{
29
30
DataSet set1 = (DataSet)Source;
31
if ((set1.Tables.Count > 1) && ((DataMember == null) || (DataMember == "")))
32
{
33
throw new Exception("If there is more than one table in your dataset, you must define the DataMember property to specify which table to use.");
34
}
35
if (set1.Tables.Count < 1)
36
{
37
throw new Exception("There are no tables in the datasource.");
38
}
39
if ((DataMember != null) && (DataMember != ""))
40
{
41
baseTable = set1.Tables[DataMember];
42
return baseTable;
43
}
44
else
45
{
46
baseTable = set1.Tables[0];
47
return baseTable;
48
}
49
50
}
51
return baseTable;
52
}
53
54
55
/**//// <summary>
56
/// 返回DataTable为哈希表键值对
57
/// </summary>
58
/// <param name="SourceTable">数据行对象</param>
59
/// <returns>填充后哈希表</returns>
60
public static Hashtable DataRowConvertHashtable(DataRow SourceRow)
61
{
62
Hashtable hTable = new Hashtable();
63
IList list = SourceRow.ItemArray;
64
object[] tObj = new object[SourceRow.Table.Columns.Count];
65
66
for (int i = 0; i < SourceRow.Table.Columns.Count; i++)
67
{
68
tObj[SourceRow.Table.Columns.IndexOf(SourceRow.Table.Columns[i].ColumnName)] = SourceRow.Table.Columns[i].ColumnName;
69
}
70
71
for (int x = 0; x < list.Count; x++)
72
{
73
hTable.Add(tObj[x].ToString(), list[x]);
74
}
75
return hTable;
76
}
77
78
/**//// <summary>
79
/// 对象转化为哈希表
80
/// </summary>
81
/// <param name="SourceTable">对象</param>
82
/// <returns>填充后哈希表</returns>
83
public static Hashtable ObjectConvertHashtable(object objModel)
84
{
85
IDomain idomain = (IDomain)objModel;
86
87
Hashtable hTable = new Hashtable();
88
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
89
for (int j = 0; j < idomain.Propertylist.Length; j++)
90
{
91
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
92
hTable.Add(idomain.Propertylist[j], propertyinfo.GetValue(objModel, null));
93
}
94
return hTable;
95
}
96
97
98
/**//// <summary>
99
/// 数据集中一行DataRow转换为指定对象,并填充数据
100
/// </summary>
101
/// <param name="row">数据集中一行</param>
102
/// <param name="objModel">指定对象</param>
103
/// <returns>填充后对象</returns>
104
public static object DataTableConvertObject(DataRow row, object objModel)
105
{
106
IDomain idomain = (IDomain)objModel;
107
108
Hashtable hTable = new Hashtable();
109
hTable = DataRowConvertHashtable(row);
110
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
111
112
for (int j = 0; j < idomain.Propertylist.Length; j++)
113
{
114
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
115
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
116
}
117
return objModel;
118
}
119
120
/**//// <summary>
121
/// 数据集中一行DataRow转换为指定对象,并填充数据
122
/// </summary>
123
/// <param name="row">数据集中一行</param>
124
/// <param name="AssemblyPath">程序集路径</param>
125
/// <param name="className">完整路径类名称</param>
126
/// <returns>填充后对象</returns>
127
public static object DataTableConvertObject(DataRow row, string AssemblyPath, string className)
128
{
129
System.Reflection.Assembly baseAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath);
130
Type entitytype = baseAssembly.GetType(className);
131
object objModel = baseAssembly.CreateInstance(className);
132
IDomain idomain = (IDomain)objModel;
133
134
Hashtable hTable = new Hashtable();
135
hTable = DataRowConvertHashtable(row);
136
137
for (int j = 0; j < idomain.Propertylist.Length; j++)
138
{
139
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
140
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
141
}
142
return objModel;
143
}
144
145
/**//// <summary>
146
/// 数据集中一行DataRow转换为指定对象,并填充数据
147
/// baseAssembly请使用:System.Reflection.Assembly.LoadFrom()获取程序集
148
/// </summary>
149
/// <param name="row">数据集中一行</param>
150
/// <param name="baseAssembly">程序集</param>
151
/// <param name="className">完整路径类名称</param>
152
/// <returns>填充后对象</returns>
153
public static object DataTableConvertObject(DataRow row, Assembly baseAssembly, string className)
154
{
155
Type entitytype = baseAssembly.GetType(className);
156
object objModel = baseAssembly.CreateInstance(className);
157
IDomain idomain = (IDomain)objModel;
158
159
Hashtable hTable = new Hashtable();
160
hTable = DataRowConvertHashtable(row);
161
162
for (int j = 0; j < idomain.Propertylist.Length; j++)
163
{
164
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
165
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
166
}
167
return objModel;
168
}
169
170
/**//// <summary>
171
/// 哈希表转换为对象
172
/// </summary>
173
/// <param name="hTable">包含数据的哈希表</param>
174
/// <param name="objModel">指定对象</param>
175
/// <returns>填充后对象</returns>
176
public static object HashtableConvertObject(Hashtable hTable, object objModel)
177
{
178
IDomain idomain = (IDomain)objModel;
179
180
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
181
182
for (int j = 0; j < idomain.Propertylist.Length; j++)
183
{
184
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
185
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
186
}
187
return objModel;
188
}
189
190
/**//// <summary>
191
/// 哈希表转换为对象
192
/// </summary>
193
/// <param name="hTable">包含数据的哈希表</param>
194
/// <param name="AssemblyPath">程序集路径</param>
195
/// <param name="className">完整路径类名称</param>
196
/// <returns>填充后对象</returns>
197
public static object HashtableConvertObject(Hashtable hTable, string AssemblyPath, string className)
198
{
199
System.Reflection.Assembly baseAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath);
200
Type entitytype = baseAssembly.GetType(className);
201
object objModel = baseAssembly.CreateInstance(className);
202
IDomain idomain = (IDomain)objModel;
203
204
for (int j = 0; j < idomain.Propertylist.Length; j++)
205
{
206
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
207
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
208
}
209
return objModel;
210
}
211
212
/**//// <summary>
213
/// 哈希表转换为对象
214
/// baseAssembly请使用:System.Reflection.Assembly.LoadFrom()获取程序集
215
/// </summary>
216
/// <param name="hTable">包含数据的哈希表</param>
217
/// <param name="baseAssembly">程序集</param>
218
/// <param name="className">完整路径类名称</param>
219
/// <returns>填充后对象</returns>
220
public static object HashtableConvertObject(Hashtable hTable, Assembly baseAssembly, string className)
221
{
222
Type entitytype = baseAssembly.GetType(className);
223
object objModel = baseAssembly.CreateInstance(className);
224
IDomain idomain = (IDomain)objModel;
225
226
for (int j = 0; j < idomain.Propertylist.Length; j++)
227
{
228
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
229
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
230
}
231
return objModel;
232
}
233
234
235
/**//// <summary>
236
/// 对象转换为DataTable,并有单行DataRow
237
/// </summary>
238
/// <param name="objModel">有数据的对象</param>
239
/// <returns>填充后数据表</returns>
240
public static DataTable ObjectConvertDataTableWidthRow(object objModel)
241
{
242
return ObjectConvertDataTableWidthRow(objModel, "");
243
}
244
245
/**//// <summary>
246
/// 对象转换为DataTable,并有单行DataRow
247
/// </summary>
248
/// <param name="objModel">有数据的对象</param>
249
/// <param name="DataMember">生成数据表名称</param>
250
/// <returns>填充后数据表</returns>
251
public static DataTable ObjectConvertDataTableWidthRow(object objModel, string DataMember)
252
{
253
IDomain idomain = (IDomain)objModel;
254
255
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
256
DataTable dt = new DataTable();
257
if (DataMember != "")
258
{
259
dt = new DataTable(DataMember);
260
}
261
dt.Columns.Clear();
262
for (int j = 0; j < idomain.Propertylist.Length; j++)
263
{
264
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
265
dt.Columns.Add(new DataColumn(idomain.Propertylist[j], propertyinfo.PropertyType));
266
}
267
DataRow row = dt.NewRow();
268
for (int j = 0; j < idomain.Propertylist.Length; j++)
269
{
270
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
271
row[idomain.Propertylist[j]] = propertyinfo.GetValue(objModel, null);
272
}
273
dt.Rows.Add(row);
274
275
return dt;
276
}
277
278
/**//// <summary>
279
/// 对象转换为DataTable,并有多行DataRow
280
/// </summary>
281
/// <param name="objModel">有数据的对象</param>
282
/// <returns>填充后数据表</returns>
283
public static DataTable ObjectConvertDataTableWidthRows(object[] objModel)
284
{
285
return ObjectConvertDataTableWidthRows(objModel, "");
286
}
287
288
/**//// <summary>
289
/// 对象转换为DataTable,并有多行DataRow
290
/// </summary>
291
/// <param name="objModel">有数据的对象</param>
292
/// <param name="DataMember">生成数据表名称</param>
293
/// <returns>填充后数据表</returns>
294
public static DataTable ObjectConvertDataTableWidthRows(object[] objModel, string DataMember)
295
{
296
IDomain[] idomain = (IDomain[])objModel;
297
298
DataTable dt = new DataTable();
299
if (objModel.Length == 0)
300
{
301
return dt;
302
}
303
if (DataMember != "")
304
{
305
dt = new DataTable(DataMember);
306
}
307
dt.Columns.Clear();
308
for (int j = 0; j < idomain[0].Propertylist.Length; j++)
309
{
310
Type entitytype = Type.GetType(objModel[0].GetType().AssemblyQualifiedName);
311
PropertyInfo propertyinfo = entitytype.GetProperty(idomain[0].Propertylist[j]);
312
dt.Columns.Add(new DataColumn(idomain[0].Propertylist[j], propertyinfo.PropertyType));
313
314
}
315
316
for (int i = 0; i < objModel.Length; i++)
317
{
318
DataRow row = dt.NewRow();
319
for (int j = 0; j < idomain[i].Propertylist.Length; j++)
320
{
321
Type entitytype = Type.GetType(objModel[i].GetType().AssemblyQualifiedName);
322
PropertyInfo propertyinfo = entitytype.GetProperty(idomain[i].Propertylist[j]);
323
row[idomain[i].Propertylist[j]] = propertyinfo.GetValue(objModel[i], null);
324
}
325
dt.Rows.Add(row);
326
}
327
return dt;
328
}
329
330
/**//// <summary>
331
/// 哈希表对象转换为DataTable,并有一行DataRow
332
/// </summary>
333
/// <param name="hTable">包含数据的哈希表</param>
334
/// <returns>填充后数据表</returns>
335
public static DataTable HashtableConvertDataTableWidthRow(Hashtable hTable)
336
{
337
return HashtableConvertDataTableWidthRow(hTable, "");
338
}
339
340
/**//// <summary>
341
/// 哈希表对象转换为DataTable,并有一行DataRow
342
/// </summary>
343
/// <param name="hTable">包含数据的哈希表</param>
344
/// <param name="DataMember">生成数据表名称</param>
345
/// <returns>填充后数据表</returns>
346
public static DataTable HashtableConvertDataTableWidthRow(Hashtable hTable, string DataMember)
347
{
348
DataTable dt = new DataTable();
349
if (DataMember != "")
350
{
351
dt = new DataTable(DataMember);
352
}
353
dt.Columns.Clear();
354
355
foreach (object key in hTable.Keys)
356
{
357
dt.Columns.Add(new DataColumn(key.ToString(), hTable[key].GetType()));
358
}
359
DataRow row = dt.NewRow();
360
foreach (object key in hTable.Keys)
361
{
362
string rowName = key.ToString();
363
row[rowName] = hTable[key];
364
}
365
dt.Rows.Add(row);
366
367
return dt;
368
}
369
370
#endregion
371
}
372
373
/**//// <summary>
374
/// IDomain接口 for 所有对象层。
375
/// </summary>
376
public interface IDomain
377
{
378
/**//// <summary>
379
/// 属性列表数组
380
/// </summary>
381
string[] Propertylist
382
{
383
get;
384
}
385
}
386
}
387
test.aspx
2

3

4

5

6

7

8

9



10

11



12


13

14


15

16

17

18

19

20



21

22

23



24

25

26

27

28



29

30

31

32



33

34

35

36



37

38

39

40



41

42

43

44

45



46

47

48

49

50

51

52

53

54

55


56

57

58

59

60

61



62

63

64

65

66

67



68

69

70

71

72



73

74

75

76

77

78


79

80

81

82

83

84



85

86

87

88

89

90



91

92

93

94

95

96

97

98


99

100

101

102

103

104

105



106

107

108

109

110

111

112

113



114

115

116

117

118

119

120


121

122

123

124

125

126

127

128



129

130

131

132

133

134

135

136

137

138



139

140

141

142

143

144

145


146

147

148

149

150

151

152

153

154



155

156

157

158

159

160

161

162

163



164

165

166

167

168

169

170


171

172

173

174

175

176

177



178

179

180

181

182

183



184

185

186

187

188

189

190


191

192

193

194

195

196

197

198



199

200

201

202

203

204

205



206

207

208

209

210

211

212


213

214

215

216

217

218

219

220

221



222

223

224

225

226

227



228

229

230

231

232

233

234

235


236

237

238

239

240

241



242

243

244

245


246

247

248

249

250

251

252



253

254

255

256

257

258



259

260

261

262

263



264

265

266

267

268

269



270

271

272

273

274

275

276

277

278


279

280

281

282

283

284



285

286

287

288


289

290

291

292

293

294

295



296

297

298

299

300



301

302

303

304



305

306

307

308

309



310

311

312

313

314

315

316

317



318

319

320



321

322

323

324

325

326

327

328

329

330


331

332

333

334

335

336



337

338

339

340


341

342

343

344

345

346

347



348

349

350



351

352

353

354

355

356



357

358

359

360

361



362

363

364

365

366

367

368

369

370

371

372

373


374

375

376

377



378


379

380

381

382



383

384

385

386

387

1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="ODRM_test" %>
2
3
<%@ Register Assembly="NickLee.Web.UI" Namespace="NickLee.Web.UI" TagPrefix="NickLee" %>
4
5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7
<html xmlns="http://www.w3.org/1999/xhtml" >
8
<head runat="server">
9
<title>Object/DataSet Relational Mapping(对象/数据集关系映射)</title>
10
</head>
11
<body>
12
<form id="form1" runat="server">
13
<div>
14
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="ODRM.doc" Target="_blank">ODRM说明文档</asp:HyperLink>
15
<br />
16
<asp:Label ID="Label1" runat="server" Height="37px" Text="以下数据由DataSet生成,但ItemDataBound做序列化DataRow处理作为演示"
17
Width="333px"></asp:Label>
18
<NickLee:ExDataGrid ID="ExDataGrid1" runat="server" OnItemDataBound="ExDataGrid1_ItemDataBound">
19
</NickLee:ExDataGrid></div>
20
</form>
21
</body>
22
</html>
23
test.aspx.cs
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
using IBatisNet.DataMapper;
13
using System.Reflection;
14
using NickLee.Common;
15
16
/**//// <summary>
17
/// Object/DataSet Relational Mapping(对象/数据集关系映射)
18
/// ODRM为结合ORM与DataSet,并自动根据O和DataSet生成对象,以便业务层处理
19
/// ODRM已经封装进NickLee.Web.UI,命名空间为NickLee.Common;
20
/// </summary>
21
public partial class ODRM_test : PageBase
22

{
23
protected void Page_Load(object sender, EventArgs e)
24
{
25
if (!IsPostBack)
26
{
27
DataSet set11 = Mapper.Instance().QueryForDataSet("SelectXTM_UserByKey_Test",UIhashtable);
28
DataTable table1 = ConvertDataTable(set11, "");
29
//这里为自己定义的序列化类
30
cXTM_User[] objModel = new cXTM_User[table1.Rows.Count];
31
//DataTable转化为序列化类数组
32
for (int y = 0; y < table1.Rows.Count; y++)
33
{
34
objModel[y] = new cXTM_User();
35
ODRM.DataTableConvertObject(table1.Rows[y], objModel[y]);
36
}
37
//以DataSet模式绑定
38
ExDataGrid1.DataSource = table1;
39
//以序列化对象模式绑定
40
//ExDataGrid1.DataSource = objModel;
41
ExDataGrid1.DataBind();
42
}
43
}
44
45
protected void ExDataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
46
{
47
/**//*
48
* 该部分应用范围
49
* 查询一条数据的修改,可以用objModel.UserName
50
* 而不必再使用DataTable[0].Rows[0]["UserName"]的模式
51
* 提高面向对象的程度,并减少业务流程部分编码
52
*/
53
54
if (e.Item.ItemIndex != -1)
55
{
56
cXTM_User objModel = new cXTM_User();
57
58
//如果为DataSet填充的DataGrid
59
if (e.Item.DataItem.GetType().FullName == "System.Data.DataRowView")
60
{
61
ODRM.DataTableConvertObject((DataRow)((DataRowView)e.Item.DataItem).Row, objModel);
62
}
63
//否则认为为序列化对象填充
64
else
65
{
66
objModel = (cXTM_User)e.Item.DataItem;
67
68
}
69
}
70
}
71
72
通用函数(如果不使用NickLee.Common.ODRM,也可以自行使用)#region 通用函数(如果不使用NickLee.Common.ODRM,也可以自行使用)
73
74
/**//// <summary>
75
/// 转换为DataTable
76
/// </summary>
77
/// <param name="Source">数据源</param>
78
/// <param name="DataMember">数据表名称</param>
79
public static DataTable ConvertDataTable(object Source, string DataMember)
80
{
81
DataTable baseTable = new DataTable();
82
if (Source is DataTable)
83
{
84
baseTable = (DataTable)Source;
85
return baseTable;
86
}
87
if (Source is DataSet)
88
{
89
90
DataSet set1 = (DataSet)Source;
91
if ((set1.Tables.Count > 1) && ((DataMember == null) || (DataMember == "")))
92
{
93
throw new Exception("If there is more than one table in your dataset, you must define the DataMember property to specify which table to use.");
94
}
95
if (set1.Tables.Count < 1)
96
{
97
throw new Exception("There are no tables in the datasource.");
98
}
99
if ((DataMember != null) && (DataMember != ""))
100
{
101
baseTable = set1.Tables[DataMember];
102
return baseTable;
103
}
104
else
105
{
106
baseTable = set1.Tables[0];
107
return baseTable;
108
}
109
110
}
111
return baseTable;
112
}
113
114
115
/**//// <summary>
116
/// 返回DataTable为哈希表键值对
117
/// </summary>
118
/// <param name="SourceTable">数据行对象</param>
119
/// <returns>填充后哈希表</returns>
120
public static Hashtable DataRowConvertHashtable(DataRow SourceRow)
121
{
122
Hashtable hTable = new Hashtable();
123
IList list = SourceRow.ItemArray;
124
object[] tObj = new object[SourceRow.Table.Columns.Count];
125
126
for (int i = 0; i < SourceRow.Table.Columns.Count; i++)
127
{
128
tObj[SourceRow.Table.Columns.IndexOf(SourceRow.Table.Columns[i].ColumnName)] = SourceRow.Table.Columns[i].ColumnName;
129
}
130
131
for (int x = 0; x < list.Count; x++)
132
{
133
hTable.Add(tObj[x].ToString(), list[x]);
134
}
135
return hTable;
136
}
137
138
/**//// <summary>
139
/// 对象转化为哈希表
140
/// </summary>
141
/// <param name="SourceTable">对象</param>
142
/// <returns>填充后哈希表</returns>
143
public static Hashtable ObjectConvertHashtable(object objModel)
144
{
145
IDomain idomain = (IDomain)objModel;
146
147
Hashtable hTable = new Hashtable();
148
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
149
for (int j = 0; j < idomain.Propertylist.Length; j++)
150
{
151
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
152
hTable.Add(idomain.Propertylist[j], propertyinfo.GetValue(objModel, null));
153
}
154
return hTable;
155
}
156
157
158
/**//// <summary>
159
/// 数据集中一行DataRow转换为指定对象,并填充数据
160
/// </summary>
161
/// <param name="row">数据集中一行</param>
162
/// <param name="objModel">指定对象</param>
163
/// <returns>填充后对象</returns>
164
public static object DataTableConvertObject(DataRow row, object objModel)
165
{
166
IDomain idomain = (IDomain)objModel;
167
168
Hashtable hTable = new Hashtable();
169
hTable = DataRowConvertHashtable(row);
170
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
171
172
for (int j = 0; j < idomain.Propertylist.Length; j++)
173
{
174
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
175
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
176
}
177
return objModel;
178
}
179
180
/**//// <summary>
181
/// 数据集中一行DataRow转换为指定对象,并填充数据
182
/// </summary>
183
/// <param name="row">数据集中一行</param>
184
/// <param name="AssemblyPath">程序集路径</param>
185
/// <param name="className">完整路径类名称</param>
186
/// <returns>填充后对象</returns>
187
public static object DataTableConvertObject(DataRow row, string AssemblyPath, string className)
188
{
189
System.Reflection.Assembly baseAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath);
190
Type entitytype = baseAssembly.GetType(className);
191
object objModel = baseAssembly.CreateInstance(className);
192
IDomain idomain = (IDomain)objModel;
193
194
Hashtable hTable = new Hashtable();
195
hTable = DataRowConvertHashtable(row);
196
197
for (int j = 0; j < idomain.Propertylist.Length; j++)
198
{
199
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
200
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
201
}
202
return objModel;
203
}
204
205
/**//// <summary>
206
/// 数据集中一行DataRow转换为指定对象,并填充数据
207
/// baseAssembly请使用:System.Reflection.Assembly.LoadFrom()获取程序集
208
/// </summary>
209
/// <param name="row">数据集中一行</param>
210
/// <param name="baseAssembly">程序集</param>
211
/// <param name="className">完整路径类名称</param>
212
/// <returns>填充后对象</returns>
213
public static object DataTableConvertObject(DataRow row, Assembly baseAssembly, string className)
214
{
215
Type entitytype = baseAssembly.GetType(className);
216
object objModel = baseAssembly.CreateInstance(className);
217
IDomain idomain = (IDomain)objModel;
218
219
Hashtable hTable = new Hashtable();
220
hTable = DataRowConvertHashtable(row);
221
222
for (int j = 0; j < idomain.Propertylist.Length; j++)
223
{
224
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
225
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
226
}
227
return objModel;
228
}
229
230
/**//// <summary>
231
/// 哈希表转换为对象
232
/// </summary>
233
/// <param name="hTable">包含数据的哈希表</param>
234
/// <param name="objModel">指定对象</param>
235
/// <returns>填充后对象</returns>
236
public static object HashtableConvertObject(Hashtable hTable, object objModel)
237
{
238
IDomain idomain = (IDomain)objModel;
239
240
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
241
242
for (int j = 0; j < idomain.Propertylist.Length; j++)
243
{
244
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
245
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
246
}
247
return objModel;
248
}
249
250
/**//// <summary>
251
/// 哈希表转换为对象
252
/// </summary>
253
/// <param name="hTable">包含数据的哈希表</param>
254
/// <param name="AssemblyPath">程序集路径</param>
255
/// <param name="className">完整路径类名称</param>
256
/// <returns>填充后对象</returns>
257
public static object HashtableConvertObject(Hashtable hTable, string AssemblyPath, string className)
258
{
259
System.Reflection.Assembly baseAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath);
260
Type entitytype = baseAssembly.GetType(className);
261
object objModel = baseAssembly.CreateInstance(className);
262
IDomain idomain = (IDomain)objModel;
263
264
for (int j = 0; j < idomain.Propertylist.Length; j++)
265
{
266
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
267
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
268
}
269
return objModel;
270
}
271
272
/**//// <summary>
273
/// 哈希表转换为对象
274
/// baseAssembly请使用:System.Reflection.Assembly.LoadFrom()获取程序集
275
/// </summary>
276
/// <param name="hTable">包含数据的哈希表</param>
277
/// <param name="baseAssembly">程序集</param>
278
/// <param name="className">完整路径类名称</param>
279
/// <returns>填充后对象</returns>
280
public static object HashtableConvertObject(Hashtable hTable, Assembly baseAssembly, string className)
281
{
282
Type entitytype = baseAssembly.GetType(className);
283
object objModel = baseAssembly.CreateInstance(className);
284
IDomain idomain = (IDomain)objModel;
285
286
for (int j = 0; j < idomain.Propertylist.Length; j++)
287
{
288
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
289
propertyinfo.SetValue(objModel, hTable[idomain.Propertylist[j]], null);
290
}
291
return objModel;
292
}
293
294
295
/**//// <summary>
296
/// 对象转换为DataTable,并有单行DataRow
297
/// </summary>
298
/// <param name="objModel">有数据的对象</param>
299
/// <returns>填充后数据表</returns>
300
public static DataTable ObjectConvertDataTableWidthRow(object objModel)
301
{
302
return ObjectConvertDataTableWidthRow(objModel, "");
303
}
304
305
/**//// <summary>
306
/// 对象转换为DataTable,并有单行DataRow
307
/// </summary>
308
/// <param name="objModel">有数据的对象</param>
309
/// <param name="DataMember">生成数据表名称</param>
310
/// <returns>填充后数据表</returns>
311
public static DataTable ObjectConvertDataTableWidthRow(object objModel, string DataMember)
312
{
313
IDomain idomain = (IDomain)objModel;
314
315
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
316
DataTable dt = new DataTable();
317
if (DataMember != "")
318
{
319
dt = new DataTable(DataMember);
320
}
321
dt.Columns.Clear();
322
for (int j = 0; j < idomain.Propertylist.Length; j++)
323
{
324
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
325
dt.Columns.Add(new DataColumn(idomain.Propertylist[j], propertyinfo.PropertyType));
326
}
327
DataRow row = dt.NewRow();
328
for (int j = 0; j < idomain.Propertylist.Length; j++)
329
{
330
PropertyInfo propertyinfo = entitytype.GetProperty(idomain.Propertylist[j]);
331
row[idomain.Propertylist[j]] = propertyinfo.GetValue(objModel, null);
332
}
333
dt.Rows.Add(row);
334
335
return dt;
336
}
337
338
/**//// <summary>
339
/// 对象转换为DataTable,并有多行DataRow
340
/// </summary>
341
/// <param name="objModel">有数据的对象</param>
342
/// <returns>填充后数据表</returns>
343
public static DataTable ObjectConvertDataTableWidthRows(object[] objModel)
344
{
345
return ObjectConvertDataTableWidthRows(objModel, "");
346
}
347
348
/**//// <summary>
349
/// 对象转换为DataTable,并有多行DataRow
350
/// </summary>
351
/// <param name="objModel">有数据的对象</param>
352
/// <param name="DataMember">生成数据表名称</param>
353
/// <returns>填充后数据表</returns>
354
public static DataTable ObjectConvertDataTableWidthRows(object[] objModel, string DataMember)
355
{
356
IDomain[] idomain = (IDomain[])objModel;
357
358
DataTable dt = new DataTable();
359
if (objModel.Length == 0)
360
{
361
return dt;
362
}
363
if (DataMember != "")
364
{
365
dt = new DataTable(DataMember);
366
}
367
dt.Columns.Clear();
368
for (int j = 0; j < idomain[0].Propertylist.Length; j++)
369
{
370
Type entitytype = Type.GetType(objModel[0].GetType().AssemblyQualifiedName);
371
PropertyInfo propertyinfo = entitytype.GetProperty(idomain[0].Propertylist[j]);
372
dt.Columns.Add(new DataColumn(idomain[0].Propertylist[j], propertyinfo.PropertyType));
373
374
}
375
376
for (int i = 0; i < objModel.Length; i++)
377
{
378
DataRow row = dt.NewRow();
379
for (int j = 0; j < idomain[i].Propertylist.Length; j++)
380
{
381
Type entitytype = Type.GetType(objModel[i].GetType().AssemblyQualifiedName);
382
PropertyInfo propertyinfo = entitytype.GetProperty(idomain[i].Propertylist[j]);
383
row[idomain[i].Propertylist[j]] = propertyinfo.GetValue(objModel[i], null);
384
}
385
dt.Rows.Add(row);
386
}
387
return dt;
388
}
389
390
/**//// <summary>
391
/// 哈希表对象转换为DataTable,并有一行DataRow
392
/// </summary>
393
/// <param name="hTable">包含数据的哈希表</param>
394
/// <returns>填充后数据表</returns>
395
public static DataTable HashtableConvertDataTableWidthRow(Hashtable hTable)
396
{
397
return HashtableConvertDataTableWidthRow(hTable, "");
398
}
399
400
/**//// <summary>
401
/// 哈希表对象转换为DataTable,并有一行DataRow
402
/// </summary>
403
/// <param name="hTable">包含数据的哈希表</param>
404
/// <param name="DataMember">生成数据表名称</param>
405
/// <returns>填充后数据表</returns>
406
public static DataTable HashtableConvertDataTableWidthRow(Hashtable hTable, string DataMember)
407
{
408
DataTable dt = new DataTable();
409
if (DataMember != "")
410
{
411
dt = new DataTable(DataMember);
412
}
413
dt.Columns.Clear();
414
415
foreach (object key in hTable.Keys)
416
{
417
dt.Columns.Add(new DataColumn(key.ToString(), hTable[key].GetType()));
418
}
419
DataRow row = dt.NewRow();
420
foreach (object key in hTable.Keys)
421
{
422
string rowName = key.ToString();
423
row[rowName] = hTable[key];
424
}
425
dt.Rows.Add(row);
426
427
return dt;
428
}
429
430
#endregion
431
}
432

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


17

18

19

20

21

22



23

24



25

26



27

28

29

30

31

32

33



34

35

36

37

38

39

40

41

42

43

44

45

46



47


48

49

50

51

52

53

54

55



56

57

58

59

60



61

62

63

64

65



66

67

68

69

70

71

72


73

74


75

76

77

78

79

80



81

82

83



84

85

86

87

88



89

90

91

92



93

94

95

96



97

98

99

100



101

102

103

104

105



106

107

108

109

110

111

112

113

114

115


116

117

118

119

120

121



122

123

124

125

126

127



128

129

130

131

132



133

134

135

136

137

138


139

140

141

142

143

144



145

146

147

148

149

150



151

152

153

154

155

156

157

158


159

160

161

162

163

164

165



166

167

168

169

170

171

172

173



174

175

176

177

178

179

180


181

182

183

184

185

186

187

188



189

190

191

192

193

194

195

196

197

198



199

200

201

202

203

204

205


206

207

208

209

210

211

212

213

214



215

216

217

218

219

220

221

222

223



224

225

226

227

228

229

230


231

232

233

234

235

236

237



238

239

240

241

242

243



244

245

246

247

248

249

250


251

252

253

254

255

256

257

258



259

260

261

262

263

264

265



266

267

268

269

270

271

272


273

274

275

276

277

278

279

280

281



282

283

284

285

286

287



288

289

290

291

292

293

294

295


296

297

298

299

300

301



302

303

304

305


306

307

308

309

310

311

312



313

314

315

316

317

318



319

320

321

322

323



324

325

326

327

328

329



330

331

332

333

334

335

336

337

338


339

340

341

342

343

344



345

346

347

348


349

350

351

352

353

354

355



356

357

358

359

360



361

362

363

364



365

366

367

368

369



370

371

372

373

374

375

376

377



378

379

380



381

382

383

384

385

386

387

388

389

390


391

392

393

394

395

396



397

398

399

400


401

402

403

404

405

406

407



408

409

410



411

412

413

414

415

416



417

418

419

420

421



422

423

424

425

426

427

428

429

430

431

432
