-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReflectionHelper.cs
More file actions
224 lines (215 loc) · 7.8 KB
/
ReflectionHelper.cs
File metadata and controls
224 lines (215 loc) · 7.8 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
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
/*
* 作用:反射获取/设置属性数据。
* */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Helper.Core.Library
{
#region 逻辑处理辅助枚举
public enum ReflectionTypeEnum : int
{
/// <summary>
/// 原始反射
/// </summary>
Original = 1,
/// <summary>
/// 表达式反射
/// </summary>
Expression = 2,
/// <summary>
/// Emit 反射
/// </summary>
Emit = 3
}
#endregion
public class ReflectionHelper
{
#region 对外公开方法
#region 创建/属性检测
/// <summary>
/// 根据 Type 创建实例
/// </summary>
/// <param name="type">Type</param>
/// <returns></returns>
public static object New(Type type)
{
return Activator.CreateInstance(type, true);
}
/// <summary>
/// 根据 Type 创建 IList 实例
/// </summary>
/// <param name="itemType">Type</param>
/// <returns></returns>
public static IList NewList(Type itemType)
{
Type newListType = typeof(List<>);
newListType = newListType.MakeGenericType(new Type[] { itemType });
return Activator.CreateInstance(newListType) as IList;
}
/// <summary>
/// 判断 Type 是否是自定义类型
/// </summary>
/// <param name="type">Type</param>
/// <returns></returns>
public static bool IsCustomType(Type type)
{
return (type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
}
/// <summary>
/// 判断 Type 是否 IList 类型
/// </summary>
/// <param name="type">Type</param>
/// <returns></returns>
public static bool IsListType(Type type)
{
return (type.GetInterface("IList") != null);
}
/// <summary>
/// 判断 Type 是否包含属性
/// </summary>
/// <param name="type">Type</param>
/// <param name="propertyName">属性名称</param>
/// <returns></returns>
public static bool IsContainProperty(Type type, string propertyName)
{
return type.GetProperty(propertyName) != null;
}
/// <summary>
/// 获得 List<T> 中 T 的类型
/// </summary>
/// <param name="propertyInfo">PropertyInfo</param>
/// <returns></returns>
public static Type GetListGenericType(PropertyInfo propertyInfo)
{
PropertyInfo[] childPropertyList = propertyInfo.PropertyType.GetProperties();
foreach (PropertyInfo childPropertyInfo in childPropertyList)
{
if (childPropertyInfo.Name == "Item") return childPropertyInfo.PropertyType;
}
return null;
}
/// <summary>
/// 获取泛型对象的 T 类型
/// </summary>
/// <param name="type">Type</param>
/// <returns></returns>
public static Type GetGenericType(Type type)
{
Type itemType = type.GetGenericArguments()[0];
return itemType;
}
#endregion
#region 属性列表获取/遍历
/// <summary>
/// 属性列表遍历
/// </summary>
/// <param name="callback">属性处理函数</param>
/// <param name="type">Type</param>
public static void Foreach(Action<PropertyInfo> callback, Type type)
{
PropertyInfo[] propertyList = type.GetProperties();
foreach (PropertyInfo propertyInfo in propertyList)
{
callback(propertyInfo);
}
}
/// <summary>
/// 获取属性名和属性值
/// </summary>
/// <param name="data">实体数据</param>
/// <returns></returns>
public static Dictionary<string, object> GetPropertyDict(object data)
{
if (data == null) return null;
Dictionary<string, object> resultDict = new Dictionary<string, object>();
Foreach((PropertyInfo propertyInfo) =>
{
resultDict.Add(propertyInfo.Name, propertyInfo.GetValue(data));
}, data.GetType());
return resultDict;
}
#endregion
#region 原始反射获取设置属性值
/// <summary>
/// 获取索引值
/// </summary>
/// <param name="data">实体数据</param>
/// <param name="indexName">索引器名称</param>
/// <returns></returns>
public static object GetPropertyIndexValue(object data, object indexName)
{
var method = data.GetType().GetMethod("get_Item", new Type[] { indexName.GetType() });
return method.Invoke(data, new object[] { indexName });
}
/// <summary>
/// 获取属性值
/// </summary>
/// <param name="data">实体数据</param>
/// <param name="propertyInfo">PropertyInfo</param>
/// <returns></returns>
public static object GetPropertyValue(object data, PropertyInfo propertyInfo)
{
if (propertyInfo == null) return null;
return propertyInfo.GetValue(data, null);
}
/// <summary>
/// 获取属性值
/// </summary>
/// <param name="data">实体数据</param>
/// <param name="propertyName">属性名称</param>
/// <returns></returns>
public static object GetPropertyValue(object data, string propertyName)
{
if (data == null) return null;
return GetPropertyValue(data, data.GetType().GetProperty(propertyName));
}
/// <summary>
/// 设置属性值
/// </summary>
/// <param name="data">实体数据</param>
/// <param name="propertyValue">要设置的数据</param>
/// <param name="propertyName">属性名称</param>
public static void SetPropertyValue(object data, object propertyValue, string propertyName)
{
SetPropertyValue(data, propertyValue, data.GetType().GetProperty(propertyName));
}
/// <summary>
/// 设置属性值
/// </summary>
/// <param name="data">实体数据</param>
/// <param name="propertyValue">要设置的数据</param>
/// <param name="propertyInfo">PropertyInfo</param>
/// <returns></returns>
public static void SetPropertyValue(object data, object propertyValue, PropertyInfo propertyInfo)
{
if (propertyValue == null) return;
if (propertyValue.GetType() == typeof(string))
{
if (propertyInfo.PropertyType == typeof(int))
{
data.GetType().GetProperty(propertyInfo.Name).SetValue(data, int.Parse(propertyValue.ToString()), null);
}
else if (propertyInfo.PropertyType == typeof(string))
{
data.GetType().GetProperty(propertyInfo.Name).SetValue(data, propertyValue, null);
}
else if (propertyInfo.PropertyType == typeof(float))
{
data.GetType().GetProperty(propertyInfo.Name).SetValue(data, float.Parse(propertyValue.ToString()), null);
}
else if (propertyInfo.PropertyType == typeof(DateTime))
{
data.GetType().GetProperty(propertyInfo.Name).SetValue(data, DateTime.Parse(propertyValue.ToString()), null);
}
}
else
{
data.GetType().GetProperty(propertyInfo.Name).SetValue(data, propertyValue, null);
}
}
#endregion
#endregion
}
}