I am looking to create a cachable lambda expression just from a methodinfo object retrieved via Type.GetMethod() without coding a typed cast of the function.
I have gotten everthing to work except for the cast from an compiled expression to an typed invokable function.
var parameters = Array.ConvertAll(method.GetParameters(), input => Expression.Parameter(input.ParameterType));
var instanceExp = Expression.Constant(_implementation);
var call = Expression.Call(instanceExp, method, parameters);
var exp = Expression.Lambda(call, parameters).Compile();
What is missing is:
Func<T1,T2,T3> castExp = (Func<T1,T2,T3>)exp;
What I would like to do is cast to a function with a specific number of parameters without specifying the specic type:
Func<object,object,object> castExp = (Func<object,object,object>)exp;
This way I could call exp(o1, o2, o3) without ever coding the types of o1 etc. But there is a runtime error casting a function of type Func to Func.
How can I cast the function to some form of func<,,> that allows for passing parameters of unspecified type?
(Btw.: It is not an option to change the signature of the methods which are to be called.)