参数将是Expression<Func<Customer,string>> selector。可以通过平面编译来读取它:
Func<Customer,string> func = selector.Compile();
然后您就可以访问func(customer)。分配比较棘手;对于简单的选择器,您可能希望可以将其分解为:
var prop = (PropertyInfo)((MemberExpression)selector.Body).Member;
prop.SetValue(customer, newValue, null);
但是,更复杂的表达式要么需要手动遍历,要么需要某些4.0表达式节点类型:
Expression<Func<Customer, string>> email
= cust => cust.Email;
var newValue = Expression.Parameter(email.Body.Type);
var assign = Expression.Lambda<Action<Customer, string>>(
Expression.Assign(email.Body, newValue),
email.Parameters[0], newValue);
var getter = email.Compile();
var setter = assign.Compile();