1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
using System;
|
28
|
|
using System.Collections;
|
29
|
|
using System.Collections.Specialized;
|
30
|
|
using System.Reflection;
|
31
|
|
|
32
|
|
using IBatisNet.Common.Exceptions;
|
33
|
|
|
34
|
|
namespace IBatisNet.Common.Utilities.Objects
|
35
|
|
{
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
public class ReflectionInfo
|
41
|
|
{
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
public static BindingFlags BINDING_FLAGS_GET
|
46
|
|
= BindingFlags.Public
|
47
|
|
| BindingFlags.GetProperty
|
48
|
|
| BindingFlags.Instance
|
49
|
|
| BindingFlags.GetField
|
50
|
|
;
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
public static BindingFlags BINDING_FLAGS_SET
|
56
|
|
= BindingFlags.Public
|
57
|
|
| BindingFlags.SetProperty
|
58
|
|
| BindingFlags.Instance
|
59
|
|
| BindingFlags.SetField
|
60
|
|
;
|
61
|
|
|
62
|
|
private static readonly string[] _emptyStringArray = new string[0];
|
63
|
|
private static ArrayList _simleTypeMap = new ArrayList();
|
64
|
|
private static Hashtable _reflectionInfoMap = Hashtable.Synchronized(new Hashtable());
|
65
|
|
|
66
|
|
private string _className = string.Empty;
|
67
|
|
private string[] _readablePropertyNames = _emptyStringArray;
|
68
|
|
private string[] _writeablePropertyNames = _emptyStringArray;
|
69
|
|
|
70
|
|
private Hashtable _setProperties = new Hashtable();
|
71
|
|
|
72
|
|
private Hashtable _getProperties = new Hashtable();
|
73
|
|
|
74
|
|
private Hashtable _setTypes = new Hashtable();
|
75
|
|
|
76
|
|
private Hashtable _getTypes = new Hashtable();
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
0
|
public string ClassName
|
82
|
|
{
|
83
|
|
get
|
84
|
|
{
|
85
|
|
return _className;
|
86
|
|
}
|
87
|
|
}
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
1
|
static ReflectionInfo()
|
93
|
|
{
|
94
|
1
|
_simleTypeMap.Add(typeof(string));
|
95
|
1
|
_simleTypeMap.Add(typeof(byte));
|
96
|
1
|
_simleTypeMap.Add(typeof(char));
|
97
|
1
|
_simleTypeMap.Add(typeof(bool));
|
98
|
1
|
_simleTypeMap.Add(typeof(Guid));
|
99
|
1
|
_simleTypeMap.Add(typeof(Int16));
|
100
|
1
|
_simleTypeMap.Add(typeof(Int32));
|
101
|
1
|
_simleTypeMap.Add(typeof(Int64));
|
102
|
1
|
_simleTypeMap.Add(typeof(Single));
|
103
|
1
|
_simleTypeMap.Add(typeof(Double));
|
104
|
1
|
_simleTypeMap.Add(typeof(Decimal));
|
105
|
1
|
_simleTypeMap.Add(typeof(DateTime));
|
106
|
1
|
_simleTypeMap.Add(typeof(TimeSpan));
|
107
|
1
|
_simleTypeMap.Add(typeof(Hashtable));
|
108
|
1
|
_simleTypeMap.Add(typeof(SortedList));
|
109
|
1
|
_simleTypeMap.Add(typeof(ListDictionary));
|
110
|
1
|
_simleTypeMap.Add(typeof(HybridDictionary));
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
|
|
115
|
|
|
116
|
|
|
117
|
1
|
_simleTypeMap.Add(typeof(ArrayList));
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
1
|
_simleTypeMap.Add(typeof(IEnumerator));
|
122
|
|
}
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
|
|
128
|
16
|
private ReflectionInfo(Type type)
|
129
|
|
{
|
130
|
16
|
_className = type.Name;
|
131
|
16
|
AddPropertiess(type);
|
132
|
|
|
133
|
16
|
string[] getArray = new string[_getProperties.Count];
|
134
|
16
|
_getProperties.Keys.CopyTo(getArray,0);
|
135
|
16
|
_readablePropertyNames = getArray;
|
136
|
|
|
137
|
16
|
string[] setArray = new string[_setProperties.Count];
|
138
|
16
|
_setProperties.Keys.CopyTo(setArray,0);
|
139
|
16
|
_writeablePropertyNames = setArray;
|
140
|
|
}
|
141
|
|
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
16
|
private void AddPropertiess(Type type)
|
147
|
|
{
|
148
|
16
|
PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
|
149
|
100
|
for (int i = 0; i < properties.Length; i++)
|
150
|
|
{
|
151
|
84
|
string name = properties[i].Name;
|
152
|
84
|
_setProperties.Add(name, properties[i]);
|
153
|
84
|
_setTypes.Add(name, properties[i].PropertyType);
|
154
|
|
}
|
155
|
|
|
156
|
16
|
properties = type.GetProperties(BINDING_FLAGS_GET);
|
157
|
100
|
for (int i = 0; i < properties.Length; i++)
|
158
|
|
{
|
159
|
84
|
string name = properties[i].Name;
|
160
|
84
|
_getProperties.Add(name, properties[i]);
|
161
|
84
|
_getTypes.Add(name, properties[i].PropertyType);
|
162
|
|
}
|
163
|
|
}
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
26229
|
public PropertyInfo GetSetter(string propertyName)
|
171
|
|
{
|
172
|
26229
|
PropertyInfo propertyInfo = (PropertyInfo) _setProperties[propertyName];
|
173
|
26229
|
if (propertyInfo == null)
|
174
|
|
{
|
175
|
0
|
throw new ProbeException("There is no Set property named '" + propertyName + "' in class '" + _className + "'");
|
176
|
|
}
|
177
|
26229
|
return propertyInfo;
|
178
|
|
}
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
514
|
public PropertyInfo GetGetter(string propertyName)
|
186
|
|
{
|
187
|
514
|
PropertyInfo propertyInfo = (PropertyInfo) _getProperties[propertyName];
|
188
|
514
|
if (propertyInfo == null)
|
189
|
|
{
|
190
|
1
|
throw new ProbeException("There is no Get property named '" + propertyName + "' in class '" + _className + "'");
|
191
|
|
}
|
192
|
513
|
return propertyInfo;
|
193
|
|
}
|
194
|
|
|
195
|
|
|
196
|
|
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
268
|
public Type GetSetterType(string propertyName)
|
201
|
|
{
|
202
|
268
|
Type type = (Type) _setTypes[propertyName];
|
203
|
268
|
if (type == null)
|
204
|
|
{
|
205
|
0
|
throw new ProbeException("There is no Set property named '" + propertyName + "' in class '" + _className + "'");
|
206
|
|
}
|
207
|
268
|
return type;
|
208
|
|
}
|
209
|
|
|
210
|
|
|
211
|
|
|
212
|
|
|
213
|
|
|
214
|
|
|
215
|
29070
|
public Type GetGetterType(string propertyName)
|
216
|
|
{
|
217
|
29070
|
Type type = (Type) _getTypes[propertyName];
|
218
|
29070
|
if (type == null)
|
219
|
|
{
|
220
|
0
|
throw new ProbeException("There is no Get property named '" + propertyName + "' in class '" + _className + "'");
|
221
|
|
}
|
222
|
29070
|
return type;
|
223
|
|
}
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
|
|
229
|
0
|
public string[] GetReadablePropertyNames()
|
230
|
|
{
|
231
|
|
return _readablePropertyNames;
|
232
|
|
}
|
233
|
|
|
234
|
|
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
69
|
public string[] GetWriteablePropertyNames()
|
239
|
|
{
|
240
|
69
|
return _writeablePropertyNames;
|
241
|
|
}
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
0
|
public bool HasWritableProperty(string propertyName)
|
249
|
|
{
|
250
|
|
return _setProperties.ContainsKey(propertyName);
|
251
|
|
}
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
2
|
public bool HasReadableProperty(string propertyName)
|
259
|
|
{
|
260
|
2
|
return _getProperties.ContainsKey(propertyName);
|
261
|
|
}
|
262
|
|
|
263
|
|
|
264
|
|
|
265
|
|
|
266
|
|
|
267
|
|
|
268
|
0
|
public static bool IsKnownType(Type type)
|
269
|
|
{
|
270
|
|
if (_simleTypeMap.Contains(type))
|
271
|
|
{
|
272
|
|
return true;
|
273
|
|
}
|
274
|
|
else if (typeof(IList).IsAssignableFrom(type))
|
275
|
|
{
|
276
|
|
return true;
|
277
|
|
}
|
278
|
|
else if (typeof(IDictionary).IsAssignableFrom(type))
|
279
|
|
{
|
280
|
|
return true;
|
281
|
|
}
|
282
|
|
else if (typeof(IEnumerator).IsAssignableFrom(type))
|
283
|
|
{
|
284
|
|
return true;
|
285
|
|
}
|
286
|
|
else
|
287
|
|
{
|
288
|
|
return false;
|
289
|
|
}
|
290
|
|
}
|
291
|
|
|
292
|
|
|
293
|
|
|
294
|
|
|
295
|
|
|
296
|
|
|
297
|
|
|
298
|
55509
|
public static ReflectionInfo GetInstance(Type type)
|
299
|
|
{
|
300
|
55509
|
lock (type)
|
301
|
|
{
|
302
|
55509
|
ReflectionInfo cache = (ReflectionInfo) _reflectionInfoMap[type];
|
303
|
55509
|
if (cache == null)
|
304
|
|
{
|
305
|
16
|
cache = new ReflectionInfo(type);
|
306
|
16
|
_reflectionInfoMap.Add(type, cache);
|
307
|
|
}
|
308
|
55509
|
return cache;
|
309
|
|
}
|
310
|
|
}
|
311
|
|
|
312
|
|
|
313
|
|
}
|
314
|
|
|
315
|
|
}
|
316
|
|
|