我是靠谱客的博主 愤怒小兔子,这篇文章主要介绍.net对反射(Type)的应用实现遍历类的属性和值并实现简单的增删改查模板,现在分享给大家,希望可以做个参考。

作用反射可以通过程序操作实体类的属性值,对实体类遍历输出更改

1.反射的基本属性和方法的用法

复制代码
1
2
3
4
5
6
7
8
9
10
Type t = typeof(T);//获取T类的反射 T model=new T(); t.GetProperties();获取类的属性集合 t.Name//获取类名 t.GetField("id");//获取字段 t.GetProperty("ID")//获取指定属性 t.GetProperty("ID").GetValue(model, null)//获取指定属性实体类model的值 t.GetProperty("ID").PropertyType == typeof(string)//判断指定属性类型 t.GetProperty("ID").SetValue(model, dr[item.ColumnName])//在model中为指定属性赋值

2.简单模板的书写

复制代码
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
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
public class ICustomBaseDAL<T> :IBaseDAL<T> where T : new() { //重构返回model的方法 public T Result(DataTable dt, Type t, DataRow dr) { T model = new T(); foreach (DataColumn item in dt.Columns) { //获取中获值t.GetProperty(item.ColumnName)如果包韩则不为空 PropertyInfo proper = t.GetProperty(item.ColumnName); if (proper != null) { //如果为空给model付null if (dr[item.ColumnName].ToString()=="") { proper.SetValue(model,null); continue; } //将值赋给model proper.SetValue(model, dr[item.ColumnName]);//设置值第一个参数设置到的实体类第二个设置的值 } } return model; } //判断是为可用类型过滤不可用属性 public bool GetWhere(Type t, PropertyInfo pope) { //以Extend结尾跳出循环 if (pope.Name.EndsWith("Extend"))//自己扩展的属性不参与查询 { return false; } //将能判断的都加进去 Type[] types = {typeof(string), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int), typeof(double), typeof(double?), typeof(decimal), typeof(decimal?) }; if (types.Contains(t)) { return true; } return false; } public virtual int Update(T model)//根据model修改必须有id { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); List<string> fields = new List<string>(); List<string> values = new List<string>(); string tableName = t.Name;//获取类的名称 PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id string idValue = pID.GetValue(model, null).ToString();//获取属性id的值//model是指从实体对象中获取 foreach (PropertyInfo pope in popes) { string proName = pope.Name; //pope.PropertyType获取属性类型 bool whereOk = GetWhere(pope.PropertyType, pope); if (!whereOk) { continue; } if (proName == "ID") { continue; } object val = pope.GetValue(model, null); //if (val == null) //{ // continue; //} if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?)) { values.Add(proName + "='" + val + "'"); } else { values.Add(proName + "=" + val + ""); } } string value = string.Join(",", values.ToArray()); string sql = " update [" + tableName + "] set " + value + " where id= " + idValue; return SqlHelper.Curtsql(sql); } public virtual int Add(T model)//添加一条数据 { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); List<string> fields = new List<string>(); List<string> values = new List<string>(); string tableName = t.Name;//获取类的名称 //PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id //string idValue = pID.GetValue(model, null).ToString();//获取属性id的值 foreach (PropertyInfo pope in popes) { string proName = pope.Name; //pope.PropertyType获取属性类型 bool whereOk = GetWhere(pope.PropertyType,pope); if (!whereOk) { continue; } if (proName == "ID") { continue; } object val = pope.GetValue(model, null); if (val != null) { fields.Add(proName); if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?)) { values.Add("'" + val + "'"); } else { values.Add("" + val + ""); } } } string field = string.Join(",", fields.ToArray()); string value = string.Join(",", values.ToArray()); string sql = "insert into [" + tableName + "] (" + field + ") values(" + value + ") "; return SqlHelper.Curtsql(sql); } public virtual int Delete(int id)//更具id删除 { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); // List<string> fields = new List<string>(); // List<string> values = new List<string>(); string tableName = t.Name;//获取类的名称 // PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id //string idValue = pID.GetValue(model, null).ToString();//获取属性id的值 string sql = " delete from [" + tableName + "] where id=" + id; return SqlHelper.Curtsql(sql); } public virtual T Search(int id)//根据id查询 { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); string filter = "*"; string tableName = "[" + t.Name + "]";//获取类的名称 string where = " and id=" + id; DataTable dt = SqlHelper.Search(filter, tableName, where); DataRow dr = dt.Rows[0]; T model = Result(dt, t, dr); return model; } public string GetWhereModel(T model)//够造where条件 { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); StringBuilder values = new StringBuilder(); if (model != null) { foreach (PropertyInfo pope in popes) { string proName = pope.Name; //pope.PropertyType获取属性类型 bool whereOk = GetWhere(pope.PropertyType,pope); if (!whereOk) { continue; } object val = pope.GetValue(model, null); if (val == null) { continue; } if (pope.PropertyType == typeof(int) || pope.PropertyType == typeof(int?)) { if (val.ToString() == "0" || val.ToString() == "-1") { continue; } } if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?)) { values.Append(" and " + proName + " like '%" + val + "%'"); } else { values.Append(" and " + proName + "=" + val + ""); } } } return values.ToString(); } public virtual List<T> DataByPager(T model, int pageSize, int pageIndex, bool isAsc, out int count)//分页查询 { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); string where = GetWhereModel(model); List<T> list = new List<T>(); QueryBy query = new QueryBy(); query.Table = "[" + t.Name + "]"; query.Filter = "*"; query.Where = where; query.sortColumn = "ID"; query.IsAsc = isAsc; query.PageSize = pageSize; query.PageIndex = pageIndex; DataTable dt = CreateDateByPager.dataByPager(query); count = query.Count; foreach (DataRow dr in dt.Rows) { T _model = Result(dt, t, dr); list.Add(_model); } return list; } //根据where条件查询内部构造where语句查询所有字段 public virtual List<T> Search(T model) { Type t = typeof(T); List<T> list = new List<T>(); string filter = "*"; string tableName = "[" + t.Name + "]";//获取类的名称 string where = GetWhereModel(model); DataTable dt = SqlHelper.Search(filter, tableName, where); foreach (DataRow dr in dt.Rows) { T _model = Result(dt, t, dr); list.Add(_model); } return list; } //根据指定条件修改 public int Update(T model, string[] propertyNames) { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); List<string> fields = new List<string>(); List<string> values = new List<string>(); string tableName = t.Name;//获取类的名称 PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id string idValue = pID.GetValue(model, null).ToString();//获取属性id的值//model是指从实体对象中获取 foreach (PropertyInfo pope in popes) { string proName = pope.Name; bool whereOk = GetWhere(pope.PropertyType,pope); if (!whereOk) { continue; } if (proName == "ID") { continue; } object val = pope.GetValue(model, null); //if (val == null) //{ // continue; //} //判断是否包含修改的元素 if (propertyNames.Contains(proName)) { if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?)) { values.Add(proName + "='" + val + "'"); } else { values.Add(proName + "=" + val + ""); } } } string value = string.Join(",", values.ToArray()); string sql = " update [" + tableName + "] set " + value + " where id= " + idValue; return SqlHelper.Curtsql(sql); } //实现批量删除 public int Delete(string ids) { Type t = typeof(T); PropertyInfo[] popes = t.GetProperties(); // List<string> fields = new List<string>(); // List<string> values = new List<string>(); string tableName = t.Name;//获取类的名称 // PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id //string idValue = pID.GetValue(model, null).ToString();//获取属性id的值 string sql = " delete from [" + tableName + "] where id in ("+ids+")"; return SqlHelper.Curtsql(sql); } }

最后

以上就是愤怒小兔子最近收集整理的关于.net对反射(Type)的应用实现遍历类的属性和值并实现简单的增删改查模板的全部内容,更多相关.net对反射(Type)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部