Mercurial > pub > bltoolkit
comparison Source/Reflection/MemberAccessor.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Data.SqlTypes; | |
3 using System.Linq.Expressions; | |
4 using System.Reflection; | |
5 using System.ComponentModel; | |
6 using BLToolkit.Data.Linq; | |
7 | |
8 #if !SILVERLIGHT && !DATA | |
9 using BLToolkit.ComponentModel; | |
10 #endif | |
11 | |
12 namespace BLToolkit.Reflection | |
13 { | |
14 public abstract class MemberAccessor | |
15 { | |
16 protected MemberAccessor(TypeAccessor typeAccessor, MemberInfo memberInfo) | |
17 { | |
18 TypeAccessor = typeAccessor; | |
19 MemberInfo = memberInfo; | |
20 } | |
21 | |
22 #region Public Properties | |
23 | |
24 public MemberInfo MemberInfo { get; private set; } | |
25 public TypeAccessor TypeAccessor { get; private set; } | |
26 | |
27 #if !SILVERLIGHT && !DATA | |
28 | |
29 private PropertyDescriptor _propertyDescriptor; | |
30 public PropertyDescriptor PropertyDescriptor | |
31 { | |
32 get | |
33 { | |
34 if (_propertyDescriptor == null) | |
35 _propertyDescriptor = new MemberPropertyDescriptor(TypeAccessor.OriginalType, Name); | |
36 | |
37 return _propertyDescriptor; | |
38 } | |
39 } | |
40 | |
41 #endif | |
42 | |
43 public virtual bool HasGetter { get { return false; } } | |
44 public virtual bool HasSetter { get { return false; } } | |
45 | |
46 public Type Type | |
47 { | |
48 get | |
49 { | |
50 return MemberInfo is PropertyInfo? | |
51 ((PropertyInfo)MemberInfo).PropertyType: | |
52 ((FieldInfo) MemberInfo).FieldType; | |
53 } | |
54 } | |
55 | |
56 public string Name | |
57 { | |
58 get { return MemberInfo.Name; } | |
59 } | |
60 | |
61 private Type _underlyingType; | |
62 public Type UnderlyingType | |
63 { | |
64 get { return _underlyingType ?? (_underlyingType = TypeHelper.GetUnderlyingType(Type)); } | |
65 } | |
66 | |
67 #endregion | |
68 | |
69 #region Public Methods | |
70 | |
71 public bool IsDefined<T>() where T : Attribute | |
72 { | |
73 return MemberInfo.IsDefined(typeof(T), true); | |
74 } | |
75 | |
76 [Obsolete("Use generic version instead")] | |
77 public Attribute GetAttribute(Type attributeType) | |
78 { | |
79 var attrs = MemberInfo.GetCustomAttributes(attributeType, true); | |
80 | |
81 return attrs.Length > 0? (Attribute)attrs[0]: null; | |
82 } | |
83 | |
84 public T GetAttribute<T>() where T : Attribute | |
85 { | |
86 var attrs = MemberInfo.GetCustomAttributes(typeof(T), true); | |
87 | |
88 return attrs.Length > 0? (T)attrs[0]: null; | |
89 } | |
90 | |
91 [Obsolete("Use generic version instead")] | |
92 public object[] GetAttributes(Type attributeType) | |
93 { | |
94 var attrs = MemberInfo.GetCustomAttributes(attributeType, true); | |
95 | |
96 return attrs.Length > 0? attrs: null; | |
97 } | |
98 | |
99 public T[] GetAttributes<T>() where T : Attribute | |
100 { | |
101 Array attrs = MemberInfo.GetCustomAttributes(typeof(T), true); | |
102 | |
103 return attrs.Length > 0? (T[])attrs: null; | |
104 } | |
105 | |
106 public object[] GetAttributes() | |
107 { | |
108 var attrs = MemberInfo.GetCustomAttributes(true); | |
109 | |
110 return attrs.Length > 0? attrs: null; | |
111 } | |
112 | |
113 public object[] GetTypeAttributes(Type attributeType) | |
114 { | |
115 return TypeHelper.GetAttributes(TypeAccessor.OriginalType, attributeType); | |
116 } | |
117 | |
118 #endregion | |
119 | |
120 #region Set/Get Value | |
121 | |
122 public virtual Boolean IsNull(object o) | |
123 { | |
124 return true; | |
125 } | |
126 | |
127 static object GetDefaultValue<T>() | |
128 { | |
129 return default(T); | |
130 } | |
131 | |
132 object _defaultValue; | |
133 | |
134 public virtual object GetValue(object o) | |
135 { | |
136 if (_defaultValue == null && Type.IsValueType && !TypeHelper.IsNullableType(Type)) | |
137 { | |
138 var mi = ReflectionHelper.Expressor<object>.MethodExpressor(_ => GetDefaultValue<int>()); | |
139 | |
140 _defaultValue = | |
141 Expression.Lambda<Func<object>>( | |
142 Expression.Call(mi.GetGenericMethodDefinition().MakeGenericMethod(Type))) | |
143 .Compile()(); | |
144 } | |
145 | |
146 return _defaultValue; | |
147 } | |
148 | |
149 public virtual void SetValue(object o, object value) | |
150 { | |
151 } | |
152 | |
153 public virtual void CloneValue(object source, object dest) | |
154 { | |
155 var value = GetValue(source); | |
156 | |
157 SetValue(dest, value is ICloneable? ((ICloneable)value).Clone(): value); | |
158 } | |
159 | |
160 | |
161 // Simple types getters. | |
162 // | |
163 [CLSCompliant(false)] | |
164 public virtual SByte GetSByte (object o) { return (SByte) GetValue(o); } | |
165 public virtual Int16 GetInt16 (object o) { return (Int16) GetValue(o); } | |
166 public virtual Int32 GetInt32 (object o) { return (Int32) GetValue(o); } | |
167 public virtual Int64 GetInt64 (object o) { return (Int64) GetValue(o); } | |
168 | |
169 public virtual Byte GetByte (object o) { return (Byte) GetValue(o); } | |
170 [CLSCompliant(false)] | |
171 public virtual UInt16 GetUInt16 (object o) { return (UInt16) GetValue(o); } | |
172 [CLSCompliant(false)] | |
173 public virtual UInt32 GetUInt32 (object o) { return (UInt32) GetValue(o); } | |
174 [CLSCompliant(false)] | |
175 public virtual UInt64 GetUInt64 (object o) { return (UInt64) GetValue(o); } | |
176 | |
177 public virtual Boolean GetBoolean (object o) { return (Boolean) GetValue(o); } | |
178 public virtual Char GetChar (object o) { return (Char) GetValue(o); } | |
179 public virtual Single GetSingle (object o) { return (Single) GetValue(o); } | |
180 public virtual Double GetDouble (object o) { return (Double) GetValue(o); } | |
181 public virtual Decimal GetDecimal (object o) { return (Decimal) GetValue(o); } | |
182 public virtual Guid GetGuid (object o) { return (Guid) GetValue(o); } | |
183 public virtual DateTime GetDateTime(object o) { return (DateTime)GetValue(o); } | |
184 public virtual TimeSpan GetTimeSpan(object o) { return (TimeSpan)GetValue(o); } | |
185 public virtual DateTimeOffset GetDateTimeOffset(object o) { return (DateTimeOffset)GetValue(o); } | |
186 | |
187 // Nullable types getters. | |
188 // | |
189 [CLSCompliant(false)] | |
190 public virtual SByte? GetNullableSByte (object o) { return (SByte?) GetValue(o); } | |
191 public virtual Int16? GetNullableInt16 (object o) { return (Int16?) GetValue(o); } | |
192 public virtual Int32? GetNullableInt32 (object o) { return (Int32?) GetValue(o); } | |
193 public virtual Int64? GetNullableInt64 (object o) { return (Int64?) GetValue(o); } | |
194 | |
195 public virtual Byte? GetNullableByte (object o) { return (Byte?) GetValue(o); } | |
196 [CLSCompliant(false)] | |
197 public virtual UInt16? GetNullableUInt16 (object o) { return (UInt16?) GetValue(o); } | |
198 [CLSCompliant(false)] | |
199 public virtual UInt32? GetNullableUInt32 (object o) { return (UInt32?) GetValue(o); } | |
200 [CLSCompliant(false)] | |
201 public virtual UInt64? GetNullableUInt64 (object o) { return (UInt64?) GetValue(o); } | |
202 | |
203 public virtual Boolean? GetNullableBoolean (object o) { return (Boolean?) GetValue(o); } | |
204 public virtual Char? GetNullableChar (object o) { return (Char?) GetValue(o); } | |
205 public virtual Single? GetNullableSingle (object o) { return (Single?) GetValue(o); } | |
206 public virtual Double? GetNullableDouble (object o) { return (Double?) GetValue(o); } | |
207 public virtual Decimal? GetNullableDecimal (object o) { return (Decimal?) GetValue(o); } | |
208 public virtual Guid? GetNullableGuid (object o) { return (Guid?) GetValue(o); } | |
209 public virtual DateTime? GetNullableDateTime(object o) { return (DateTime?)GetValue(o); } | |
210 public virtual TimeSpan? GetNullableTimeSpan(object o) { return (TimeSpan?)GetValue(o); } | |
211 public virtual DateTimeOffset? GetNullableDateTimeOffset(object o) { return (DateTimeOffset?)GetValue(o); } | |
212 | |
213 #if !SILVERLIGHT | |
214 | |
215 // SQL type getters. | |
216 // | |
217 public virtual SqlByte GetSqlByte (object o) { return (SqlByte) GetValue(o); } | |
218 public virtual SqlInt16 GetSqlInt16 (object o) { return (SqlInt16) GetValue(o); } | |
219 public virtual SqlInt32 GetSqlInt32 (object o) { return (SqlInt32) GetValue(o); } | |
220 public virtual SqlInt64 GetSqlInt64 (object o) { return (SqlInt64) GetValue(o); } | |
221 public virtual SqlSingle GetSqlSingle (object o) { return (SqlSingle) GetValue(o); } | |
222 public virtual SqlBoolean GetSqlBoolean (object o) { return (SqlBoolean) GetValue(o); } | |
223 public virtual SqlDouble GetSqlDouble (object o) { return (SqlDouble) GetValue(o); } | |
224 public virtual SqlDateTime GetSqlDateTime(object o) { return (SqlDateTime)GetValue(o); } | |
225 public virtual SqlDecimal GetSqlDecimal (object o) { return (SqlDecimal) GetValue(o); } | |
226 public virtual SqlMoney GetSqlMoney (object o) { return (SqlMoney) GetValue(o); } | |
227 public virtual SqlGuid GetSqlGuid (object o) { return (SqlGuid) GetValue(o); } | |
228 public virtual SqlString GetSqlString (object o) { return (SqlString) GetValue(o); } | |
229 | |
230 #endif | |
231 | |
232 // Simple type setters. | |
233 // | |
234 [CLSCompliant(false)] | |
235 public virtual void SetSByte (object o, SByte value) { SetValue(o, value); } | |
236 public virtual void SetInt16 (object o, Int16 value) { SetValue(o, value); } | |
237 public virtual void SetInt32 (object o, Int32 value) { SetValue(o, value); } | |
238 public virtual void SetInt64 (object o, Int64 value) { SetValue(o, value); } | |
239 | |
240 public virtual void SetByte (object o, Byte value) { SetValue(o, value); } | |
241 [CLSCompliant(false)] | |
242 public virtual void SetUInt16 (object o, UInt16 value) { SetValue(o, value); } | |
243 [CLSCompliant(false)] | |
244 public virtual void SetUInt32 (object o, UInt32 value) { SetValue(o, value); } | |
245 [CLSCompliant(false)] | |
246 public virtual void SetUInt64 (object o, UInt64 value) { SetValue(o, value); } | |
247 | |
248 public virtual void SetBoolean (object o, Boolean value) { SetValue(o, value); } | |
249 public virtual void SetChar (object o, Char value) { SetValue(o, value); } | |
250 public virtual void SetSingle (object o, Single value) { SetValue(o, value); } | |
251 public virtual void SetDouble (object o, Double value) { SetValue(o, value); } | |
252 public virtual void SetDecimal (object o, Decimal value) { SetValue(o, value); } | |
253 public virtual void SetGuid (object o, Guid value) { SetValue(o, value); } | |
254 public virtual void SetDateTime(object o, DateTime value) { SetValue(o, value); } | |
255 public virtual void SetTimeSpan(object o, TimeSpan value) { SetValue(o, value); } | |
256 public virtual void SetDateTimeOffset(object o, DateTimeOffset value) { SetValue(o, value); } | |
257 | |
258 // Simple type setters. | |
259 // | |
260 [CLSCompliant(false)] | |
261 public virtual void SetNullableSByte (object o, SByte? value) { SetValue(o, value); } | |
262 public virtual void SetNullableInt16 (object o, Int16? value) { SetValue(o, value); } | |
263 public virtual void SetNullableInt32 (object o, Int32? value) { SetValue(o, value); } | |
264 public virtual void SetNullableInt64 (object o, Int64? value) { SetValue(o, value); } | |
265 | |
266 public virtual void SetNullableByte (object o, Byte? value) { SetValue(o, value); } | |
267 [CLSCompliant(false)] | |
268 public virtual void SetNullableUInt16 (object o, UInt16? value) { SetValue(o, value); } | |
269 [CLSCompliant(false)] | |
270 public virtual void SetNullableUInt32 (object o, UInt32? value) { SetValue(o, value); } | |
271 [CLSCompliant(false)] | |
272 public virtual void SetNullableUInt64 (object o, UInt64? value) { SetValue(o, value); } | |
273 | |
274 public virtual void SetNullableBoolean (object o, Boolean? value) { SetValue(o, value); } | |
275 public virtual void SetNullableChar (object o, Char? value) { SetValue(o, value); } | |
276 public virtual void SetNullableSingle (object o, Single? value) { SetValue(o, value); } | |
277 public virtual void SetNullableDouble (object o, Double? value) { SetValue(o, value); } | |
278 public virtual void SetNullableDecimal (object o, Decimal? value) { SetValue(o, value); } | |
279 public virtual void SetNullableGuid (object o, Guid? value) { SetValue(o, value); } | |
280 public virtual void SetNullableDateTime(object o, DateTime? value) { SetValue(o, value); } | |
281 public virtual void SetNullableTimeSpan(object o, TimeSpan? value) { SetValue(o, value); } | |
282 public virtual void SetNullableDateTimeOffset(object o, DateTimeOffset? value) { SetValue(o, value); } | |
283 | |
284 #if !SILVERLIGHT | |
285 | |
286 // SQL type setters. | |
287 // | |
288 public virtual void SetSqlByte (object o, SqlByte value) { SetValue(o, value); } | |
289 public virtual void SetSqlInt16 (object o, SqlInt16 value) { SetValue(o, value); } | |
290 public virtual void SetSqlInt32 (object o, SqlInt32 value) { SetValue(o, value); } | |
291 public virtual void SetSqlInt64 (object o, SqlInt64 value) { SetValue(o, value); } | |
292 public virtual void SetSqlSingle (object o, SqlSingle value) { SetValue(o, value); } | |
293 public virtual void SetSqlBoolean (object o, SqlBoolean value) { SetValue(o, value); } | |
294 public virtual void SetSqlDouble (object o, SqlDouble value) { SetValue(o, value); } | |
295 public virtual void SetSqlDateTime(object o, SqlDateTime value) { SetValue(o, value); } | |
296 public virtual void SetSqlDecimal (object o, SqlDecimal value) { SetValue(o, value); } | |
297 public virtual void SetSqlMoney (object o, SqlMoney value) { SetValue(o, value); } | |
298 public virtual void SetSqlGuid (object o, SqlGuid value) { SetValue(o, value); } | |
299 public virtual void SetSqlString (object o, SqlString value) { SetValue(o, value); } | |
300 | |
301 #endif | |
302 | |
303 #endregion | |
304 } | |
305 } |