Hi,
I feel like I'm so close to working this out and have read dozens of articles and existing questions.
I have a method like this:
public T DoStuff(object arg1, object arg2)
{
/* Do stuff and return a T */
}
And I need to be able to pass this method to another class for callback purposes.
However, when this other class calls back it will do so with a different expected return type that is only known at runtime.
E.g.:
public void SomeMethod(MethodInfo m, ref object caller)
{
MethodInfo callback = m.MakeGenericMethod(new Type[] { runtimeType });
callback.Invoke(caller, new [] { arg1val, arg2val });
}
/* DoStuff() will make a call to SomeMethod() passing
* itself (the method and the object) */
However, instead of this I would like to be able to
- Pass only a single callback object (i.e. a delegate or Func<>) to SomeMethod.
- Be able to modify this object appropriately for the generic call.
- Have type-safe parameters for the call as I would with a delegate/Func<>.
I've found approaches that suit each of these individually but none that covers all of them.
Is it possible?