private static LambdaExpression GenerateGetterLambda <TInterface, TState, TArg>(PropertyInfo stateProperty) { var ctxParam = Expression.Parameter(typeof(IContext <TInterface, TState>), "ctx"); var stateGetter = Expression.Property(ctxParam, PropertyReflector <IContext <TInterface, TState> > .FromGetter(s => s.State)); var statePropertyGetter = Expression.Property(stateGetter, stateProperty); return(Expression.Lambda <Func <IContext <TInterface, TState>, TArg> >(statePropertyGetter, ctxParam)); }
private static LambdaExpression GenerateSetterLambda <TInterface, TState, TArg>(PropertyInfo stateProperty) { var setMethod = stateProperty.GetSetMethod(); var ctxParam = Expression.Parameter(typeof(IContext <TInterface, TState>), "ctx"); var argParam = Expression.Parameter(typeof(TArg), "value"); var stateGetter = Expression.Property(ctxParam, PropertyReflector <IContext <TInterface, TState> > .FromGetter(s => s.State)); var statePropertySetMethod = Expression.Call(stateGetter, setMethod, argParam); return(Expression.Lambda <Action <IContext <TInterface, TState>, TArg> >(statePropertySetMethod, ctxParam, argParam)); }
public void Should_find_property_based_on_getter_method() { PropertyInfo propertyInfo = typeof(SomeClass).GetProperty("GetterOnly"); Assert.That(PropertyReflector <SomeClass> .FromGetter(c => c.GetterOnly), Is.EqualTo(propertyInfo)); }