Generically creating a delegate from a MethodInfo with parameter upcasting
I am trying to create a delegate of type
TDelegate
that allows me to invoke a MethodInfo instance with some upcasted parameters. A simple example of what i want is as follows:
The tldr, i want to take method void SomeFunction(object param)
and create a delegate Action<int>
... generically
In this example im just calling MethodInfo::CreateDelegate<TDelegate>()
- which fails. This is fine, i expect and understand why this happens. I'm just looking for ideas to achieve what im looking for.
traditionally, i might just wrap the MethodInfo invocation within a lambda and call it a day... but since TDelegate is a generic type i dont actually know the number of parameters until the CreateDelegateWithCasting
helper method is called - which is where my problem lies.1 Reply
I have been pursuing a possible solution by creating a
DynamicMethod
- the proof of concept i have at the moment looks like so:
But... im already starting to see how much work its going to take to achieve my goal. Is this already a solved problem? I feel like i cant be the first to want this - is there a better solution than the IL & reflection madness ive embarked on? Perhaps a usefil library that already did the hard work for me?
Any ideas would be hugely appreciated. thanks!