In short I'm trying to build the equivalent of
Call<UnknownRegisteredClass>(r => r.UnknownMethod(...args))
as an Expression<Action<T> via reflection at runtime.
The whole concept of Expressions is quite new to me, but everything I'm finding in the docs doesn't seem to tackle it from a generic/reflection perspective but just from a more of a building expressions with known types.
So far I have managed to rig up the inside expression r.UnknownMethod(...args):
Expression.Lambda<Action>(Expression.Call(Expression.New(actionType), actionMethod, parameterExpressions))
But I'm not sure how to wrap that in another level where that is call is actually made against the parameter of an Action<T>
Seems like it would go something like:
ParameterExpression instance = Expression.Parameter(typeof(T), "instance");
return Expression.Lambda<Action<T>>(Expression.Call(instance, HowDoIGetMyAboveExpressionHere), new ParameterExpression[] { instance });
But that doesn't really work because I can't use <T> since I don't know the type until runtime. If anyone can see what I'm trying to do and has any examples they could point me to I would really appreciate it.
Expression<Action<T>>, so I'd expect the expression you want to build takes a parameter of typeTand returns void. That is clearly not the case with theCall(...)expression you have shown. Where is the parameter of typeT?void Call<T>(Expression<Action<T>> methodCall);Just providing it a method on T that it should call. But I need to be able to do this at runtime given a request so I'm usingMakeGenericMethod(...)with the type I get since I have the MethodInfo but I'm just not sure how to build that Expression<Action<T>> to pass in. I have the example where I can do it as just Action, but I need it to call that on the first parameter that will be provided in the Action<T>.