ModelConstructor.cs
7.31 KB
1
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
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Quiz.Utility
{
/// <summary>
/// 数据模型构造器。
/// </summary>
/// <typeparam name="T"></typeparam>
public static class ModelConstructor<T> where T : class, new()
{
/// <summary>
/// 根据datarow中数据,创建一个属性与其列名相对应的数据模型。
/// </summary>
/// <param name="datarow"></param>
/// <returns></returns>
public static T CreateModel(System.Data.DataRow datarow)
{
return ModelConstructor.CreateModel(typeof(T), datarow) as T;
}
/// <summary>
/// 根据datarowview中数据,创建一个属性与其列名相对应的数据模型。
/// </summary>
/// <param name="datarowview"></param>
/// <returns></returns>
public static T CreateModel(System.Data.DataRowView datarowview)
{
return ModelConstructor.CreateModel(typeof(T), datarowview) as T;
}
/// <summary>
/// 根据datareader中当前读取行数据,创建一个属性与其列名相对应的数据模型。
/// </summary>
/// <param name="datareader"></param>
/// <returns></returns>
public static T CreateModel(System.Data.SqlClient.SqlDataReader datareader)
{
return ModelConstructor.CreateModel(typeof(T), datareader) as T;
}
/// <summary>
/// 根据datatable中数据,创建一个属性与其列名相对应的数据模型集合。
/// </summary>
/// <param name="datatable"></param>
/// <returns></returns>
public static T[] CreateModels(System.Data.DataTable datatable)
{
if (datatable == null || datatable.Rows.Count <= 0) return new T[0];
List<T> list = new List<T>();
for (int i = 0; i < datatable.Rows.Count; i++) list.Add(CreateModel(datatable.Rows[i]));
return list.ToArray();
}
/// <summary>
/// 根据datatable中数据,创建一个属性与其列名相对应的数据模型集合。
/// </summary>
/// <param name="datatable"></param>
/// <returns></returns>
public static List<T> CreateModelList(System.Data.DataTable datatable)
{
if (datatable == null || datatable.Rows.Count <= 0) return new List<T>();
List<T> list = new List<T>();
for (int i = 0; i < datatable.Rows.Count; i++) list.Add(CreateModel(datatable.Rows[i]));
return list;
}
/// <summary>
/// 根据xml中数据,创建一个属性与其列名相对应的数据模型集合。
/// </summary>
/// <param name="xml"></param>
/// <param name="createRoot"></param>
/// <returns></returns>
public static List<T> CreateModelList(string xml, bool createRoot = true)
{
try
{
if (createRoot) xml = string.Format("<root>{0}</root>", xml);
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
DataTable dt = new DataTable();
var rootnode = document.SelectSingleNode("root");
var first = rootnode.ChildNodes[0];
foreach (XmlNode childNode in first.ChildNodes)
{
dt.Columns.Add(childNode.Name);
}
foreach (XmlNode childNode in rootnode.ChildNodes)
{
DataRow row = dt.NewRow();
foreach (XmlNode node in childNode.ChildNodes)
{
row[node.Name] = node.InnerText;
}
dt.Rows.Add(row);
}
return CreateModelList(dt);
}
catch
{
return new List<T>();
}
}
}
/// <summary>
/// 数据模型构造器。
/// </summary>
static class ModelConstructor
{
internal static object CreateModel(Type modelType, System.Data.DataRow datarow)
{
object model = modelType.Assembly.CreateInstance(modelType.ToString());
System.Reflection.PropertyInfo[] pinfolist = modelType.GetProperties();
if (pinfolist == null || pinfolist.Length <= 0) return model;
for (int i = 0; i < pinfolist.Length; i++)
{
if (!datarow.Table.Columns.Contains(pinfolist[i].Name)) continue;
object value = datarow[pinfolist[i].Name];
if (value is DBNull || value == null) continue;
Type changeType = pinfolist[i].PropertyType;
if (changeType.ToString() == "System.Guid") value = new Guid(value.ToString());
else if (changeType.IsGenericType && changeType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) changeType = Nullable.GetUnderlyingType(changeType);
pinfolist[i].SetValue(model, Convert.ChangeType(value, changeType), null);
}
return model;
}
internal static object CreateModel(Type modelType, System.Data.DataRowView datarowview)
{
object model = modelType.Assembly.CreateInstance(modelType.ToString());
System.Reflection.PropertyInfo[] pinfolist = modelType.GetProperties();
if (pinfolist == null || pinfolist.Length <= 0) return model;
for (int i = 0; i < pinfolist.Length; i++)
{
if (!datarowview.DataView.Table.Columns.Contains(pinfolist[i].Name)) continue;
object value = datarowview[pinfolist[i].Name];
if (value is DBNull || value == null) continue;
Type changeType = pinfolist[i].PropertyType;
if (changeType.ToString() == "System.Guid") value = new Guid(value.ToString());
else if (changeType.IsGenericType && changeType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) changeType = Nullable.GetUnderlyingType(changeType);
pinfolist[i].SetValue(model, Convert.ChangeType(value, changeType), null);
}
return model;
}
internal static object CreateModel(Type modelType, System.Data.SqlClient.SqlDataReader datareader)
{
object model = modelType.Assembly.CreateInstance(modelType.ToString());
System.Reflection.PropertyInfo[] pinfolist = modelType.GetProperties();
if (pinfolist == null || pinfolist.Length <= 0) return model;
for (int i = 0; i < pinfolist.Length; i++)
{
if (datareader.GetOrdinal(pinfolist[i].Name) < 0) continue;
object value = datareader[pinfolist[i].Name];
if (value is DBNull || value == null) continue;
Type changeType = pinfolist[i].PropertyType;
if (changeType.ToString() == "System.Guid") value = new Guid(value.ToString());
else if (changeType.IsGenericType && changeType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) changeType = Nullable.GetUnderlyingType(changeType);
pinfolist[i].SetValue(model, Convert.ChangeType(value, changeType), null);
}
return model;
}
}
}