This is not the answer at the moment, but I would recommend you to change your code to something like this:
static Action<TOb, TPar> BuildSetAccessor<TOb, TPar>(MethodInfo method)
{
var obj = Expression.Parameter(method.DeclaringType, "o");
var value = Expression.Parameter(method.GetParameters()[0].ParameterType);
if (method.GetParameters().Length > 1)
throw new ArgumentException("Method with more than 1 parameters is not supported");
LambdaExpression expr =
Expression.Lambda(
Expression.Call(
obj,
method,
value),
obj, value);
var compiled = expr.Compile();
return new Action<TOb, TPar>((o, p) => compiled.DynamicInvoke(o, p));
}
And usage:
var method = BuildSetAccessor<Base, object>(prop.SetMethod);
method(test, "1");
I think this approach is better then casting parameters in LINQ query because generated exception info is more detailed.