public static Action<object, object> CreateSetPropertyMethod(PropertyInfo propertyInfo)
{
MethodInfo mi = propertyInfo.GetSetMethod();
if (!propertyInfo.CanWrite || mi == null)
return null;
ParameterExpression targParam = Expression.Parameter(typeof(object), "obj");
ParameterExpression valueParam = Expression.Parameter(typeof(object), "val");
//convert parameters into their proper types
UnaryExpression target = Expression.Convert(targParam, propertyInfo.DeclaringType);
UnaryExpression value = Expression.Convert(valueParam, propertyInfo.PropertyType);
//and call the setter on it.
MethodCallExpression mce = Expression.Call(target, mi, value);
return Expression.Lambda<Action<object, object>>(mce, targParam, valueParam).Compile();
}