Question

Hi I have following perfectly working code when I run it in console application but it fails in real app with exeption "Specified cast is not valid." at lambda_method(Closure , Object , Object ), do any one knows that is root cause for this ?

    public class Base
    {

    }

    public class Test:Base
    {
        public string TestProp { get; set; }
    }

    static void Main(string[] args)
    {
        Base test = new Test();
        var prop=test.GetType().GetProperty("TestProp");
        var method = BuildSetAccessor(prop.SetMethod);
        method(test, "sa");
    }

    static Action<object, object> BuildSetAccessor(MethodInfo method)
    {

        var obj = Expression.Parameter(typeof(object), "o");
        var value = Expression.Parameter(typeof(object));

        Expression<Action<object, object>> expr =
            Expression.Lambda<Action<object, object>>(
                Expression.Call(
                    Expression.Convert(obj, method.DeclaringType),
                    method,
                    Expression.Convert(value, method.GetParameters()[0].ParameterType)),
                obj, value);


        return expr.Compile();
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top