@@ -51,10 +51,12 @@ public static Func<T, object> GetValueGetter<T>(this PropertyInfo propertyInfo)
51
51
if ( getMethodInfo == null ) return null ;
52
52
return x => getMethodInfo . Invoke ( x , new object [ 0 ] ) ;
53
53
#else
54
- var instance = Expression . Parameter ( propertyInfo . DeclaringType , "i" ) ;
55
- var property = Expression . Property ( instance , propertyInfo ) ;
56
- var convert = Expression . TypeAs ( property , typeof ( object ) ) ;
57
- return Expression . Lambda < Func < T , object > > ( convert , instance ) . Compile ( ) ;
54
+ var instance = Expression . Parameter ( typeof ( T ) , "i" ) ;
55
+ var property = typeof ( T ) != propertyInfo . DeclaringType
56
+ ? Expression . Property ( Expression . TypeAs ( instance , propertyInfo . DeclaringType ) , propertyInfo )
57
+ : Expression . Property ( instance , propertyInfo ) ;
58
+ var convertProperty = Expression . TypeAs ( property , typeof ( object ) ) ;
59
+ return Expression . Lambda < Func < T , object > > ( convertProperty , instance ) . Compile ( ) ;
58
60
#endif
59
61
}
60
62
@@ -63,25 +65,28 @@ public static Func<T, object> GetValueGetter<T>(this FieldInfo fieldInfo)
63
65
#if ( SL5 && ! WP ) || __IOS__ || XBOX
64
66
return x => fieldInfo . GetValue ( x ) ;
65
67
#else
66
- var instance = Expression . Parameter ( fieldInfo . DeclaringType , "i" ) ;
67
- var property = Expression . Field ( instance , fieldInfo ) ;
68
- var convert = Expression . TypeAs ( property , typeof ( object ) ) ;
69
- return Expression . Lambda < Func < T , object > > ( convert , instance ) . Compile ( ) ;
68
+
69
+ var instance = Expression . Parameter ( typeof ( T ) , "i" ) ;
70
+ var field = typeof ( T ) != fieldInfo . DeclaringType
71
+ ? Expression . Field ( Expression . TypeAs ( instance , fieldInfo . DeclaringType ) , fieldInfo )
72
+ : Expression . Field ( instance , fieldInfo ) ;
73
+ var convertField = Expression . TypeAs ( field , typeof ( object ) ) ;
74
+ return Expression . Lambda < Func < T , object > > ( convertField , instance ) . Compile ( ) ;
70
75
#endif
71
76
}
72
77
73
78
#if ! XBOX
74
79
public static Action < T , object > GetValueSetter < T > ( this PropertyInfo propertyInfo )
75
80
{
76
- if ( typeof ( T ) != propertyInfo . DeclaringType )
77
- {
78
- throw new ArgumentException ( ) ;
79
- }
80
-
81
- var instance = Expression . Parameter ( propertyInfo . DeclaringType , "i" ) ;
81
+ var instance = Expression . Parameter ( typeof ( T ) , "i" ) ;
82
82
var argument = Expression . Parameter ( typeof ( object ) , "a" ) ;
83
+
84
+ var instanceType = typeof ( T ) != propertyInfo . DeclaringType
85
+ ? ( Expression ) Expression . TypeAs ( instance , propertyInfo . DeclaringType )
86
+ : instance ;
87
+
83
88
var setterCall = Expression . Call (
84
- instance ,
89
+ instanceType ,
85
90
propertyInfo . SetMethod ( ) ,
86
91
Expression . Convert ( argument , propertyInfo . PropertyType ) ) ;
87
92
@@ -90,6 +95,25 @@ public static Action<T, object> GetValueSetter<T>(this PropertyInfo propertyInfo
90
95
setterCall , instance , argument
91
96
) . Compile ( ) ;
92
97
}
98
+
99
+ public static Action < T , object > GetValueSetter < T > ( this FieldInfo fieldInfo )
100
+ {
101
+ var instance = Expression . Parameter ( typeof ( T ) , "i" ) ;
102
+ var argument = Expression . Parameter ( typeof ( object ) , "a" ) ;
103
+
104
+ var field = typeof ( T ) != fieldInfo . DeclaringType
105
+ ? Expression . Field ( Expression . TypeAs ( instance , fieldInfo . DeclaringType ) , fieldInfo )
106
+ : Expression . Field ( instance , fieldInfo ) ;
107
+
108
+ var setterCall = Expression . Assign (
109
+ field ,
110
+ Expression . Convert ( argument , fieldInfo . FieldType ) ) ;
111
+
112
+ return Expression . Lambda < Action < T , object > >
113
+ (
114
+ setterCall , instance , argument
115
+ ) . Compile ( ) ;
116
+ }
93
117
#endif
94
118
95
119
}
0 commit comments