@@ -51,10 +51,12 @@ public static Func<T, object> GetValueGetter<T>(this PropertyInfo propertyInfo)
5151 if ( getMethodInfo == null ) return null ;
5252 return x => getMethodInfo . Invoke ( x , new object [ 0 ] ) ;
5353#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 ( ) ;
5860#endif
5961 }
6062
@@ -63,25 +65,28 @@ public static Func<T, object> GetValueGetter<T>(this FieldInfo fieldInfo)
6365#if ( SL5 && ! WP ) || __IOS__ || XBOX
6466 return x => fieldInfo . GetValue ( x ) ;
6567#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 ( ) ;
7075#endif
7176 }
7277
7378#if ! XBOX
7479 public static Action < T , object > GetValueSetter < T > ( this PropertyInfo propertyInfo )
7580 {
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" ) ;
8282 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+
8388 var setterCall = Expression . Call (
84- instance ,
89+ instanceType ,
8590 propertyInfo . SetMethod ( ) ,
8691 Expression . Convert ( argument , propertyInfo . PropertyType ) ) ;
8792
@@ -90,6 +95,25 @@ public static Action<T, object> GetValueSetter<T>(this PropertyInfo propertyInfo
9095 setterCall , instance , argument
9196 ) . Compile ( ) ;
9297 }
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+ }
93117#endif
94118
95119 }
0 commit comments