public static Action <object, object> CreatePropertySetter(PropertyInfo property) { ParameterExpression expression; ParameterExpression expression2; if (property.ReflectedType.IsValueType) { RefSetterDelegate refHandler = CreateSetMethod(property); return(delegate(object instance, object value) { refHandler(ref instance, value); }); } MethodCallExpression body = Expression.Call(Expression.Convert(expression = Expression.Parameter(typeof(object), "instance"), property.ReflectedType), property.GetSetMethod(true), new Expression[] { Expression.Convert(expression2 = Expression.Parameter(typeof(object), "value"), property.PropertyType) }); return((Action <object, object>)Expression.Lambda(typeof(Action <object, object>), body, new ParameterExpression[] { expression, expression2 }).Compile()); }
public static Action <object, object> CreateFieldSetter(FieldInfo field) { ParameterExpression expression; ParameterExpression expression2; if (field.IsInitOnly || field.ReflectedType.IsValueType) { RefSetterDelegate refHandler = CreateSetMethod(field); return(delegate(object instance, object value) { refHandler(ref instance, value); }); } BinaryExpression body = Expression.Assign(Expression.Field(Expression.Convert(expression = Expression.Parameter(typeof(object), "instance"), field.ReflectedType), field), Expression.Convert(expression2 = Expression.Parameter(typeof(object), "value"), field.FieldType)); return((Action <object, object>)Expression.Lambda(typeof(Action <object, object>), body, new ParameterExpression[] { expression, expression2 }).Compile()); }