15
15
using System . Reflection ;
16
16
using System . Threading ;
17
17
using ServiceStack . Text ;
18
+ using ServiceStack . Text . Common ;
18
19
#if ! XBOX
19
20
using System . Linq . Expressions ;
20
21
#endif
@@ -181,6 +182,25 @@ public static Action<T, object> GetValueSetter<T>(this PropertyInfo propertyInfo
181
182
) . Compile ( ) ;
182
183
}
183
184
185
+ public static Action < object , object > GetValueSetter ( this FieldInfo fieldInfo , Type instanceType )
186
+ {
187
+ var instance = Expression . Parameter ( typeof ( object ) , "i" ) ;
188
+ var argument = Expression . Parameter ( typeof ( object ) , "a" ) ;
189
+
190
+ var field = instanceType != fieldInfo . DeclaringType
191
+ ? Expression . Field ( Expression . TypeAs ( instance , fieldInfo . DeclaringType ) , fieldInfo )
192
+ : Expression . Field ( Expression . Convert ( instance , instanceType ) , fieldInfo ) ;
193
+
194
+ var setterCall = Expression . Assign (
195
+ field ,
196
+ Expression . Convert ( argument , fieldInfo . FieldType ) ) ;
197
+
198
+ return Expression . Lambda < Action < object , object > >
199
+ (
200
+ setterCall , instance , argument
201
+ ) . Compile ( ) ;
202
+ }
203
+
184
204
public static Action < T , object > GetValueSetter < T > ( this FieldInfo fieldInfo )
185
205
{
186
206
var instance = Expression . Parameter ( typeof ( T ) , "i" ) ;
@@ -199,6 +219,25 @@ public static Action<T, object> GetValueSetter<T>(this FieldInfo fieldInfo)
199
219
setterCall , instance , argument
200
220
) . Compile ( ) ;
201
221
}
222
+
223
+ public static SetPropertyDelegateRefGeneric < T > GetValueSetterGenericRef < T > ( this FieldInfo fieldInfo )
224
+ {
225
+ var instance = Expression . Parameter ( typeof ( T ) . MakeByRefType ( ) , "i" ) ;
226
+ var argument = Expression . Parameter ( typeof ( object ) , "a" ) ;
227
+
228
+ var field = typeof ( T ) != fieldInfo . DeclaringType
229
+ ? Expression . Field ( Expression . TypeAs ( instance , fieldInfo . DeclaringType ) , fieldInfo )
230
+ : Expression . Field ( instance , fieldInfo ) ;
231
+
232
+ var setterCall = Expression . Assign (
233
+ field ,
234
+ Expression . Convert ( argument , fieldInfo . FieldType ) ) ;
235
+
236
+ return Expression . Lambda < SetPropertyDelegateRefGeneric < T > >
237
+ (
238
+ setterCall , instance , argument
239
+ ) . Compile ( ) ;
240
+ }
202
241
#endif
203
242
204
243
}
0 commit comments