I am fairly new to using the Expression namespace and was wondering if anyone can share their knowledge. I have created a generic class RuleSet which has the following method. I am using the method to register a function against a property that belongs to type T, this check isn't in place, after extracting the property i invoke the rule, passing the value from the property. Finally i have compiled the expression so that i can use it later during validation. Is this the best way to formulate the expression?
Are there any tips or tricks that can be used to build and debug expressions?
public void Register<TProperty>(Expression<Func<T,TProperty>> property, Func<T, TProperty, bool> rule)
{
var instanceParameter = Expression.Parameter(typeof (T), "instance");
Expression<Func<T, TProperty, bool>> ruleExpression = (i, p) => rule.Invoke(i,p);
var invoker = Expression.Invoke(ruleExpression, instanceParameter, Expression.Invoke(property, instanceParameter));
_rules.Add((Func<T,bool>)Expression.Lambda(invoker, instanceParameter).Compile());
}