Is there a way to store a delegate without binding it to an object like how you can with a MethodInfo? Right now I am storing a MethodInfo so I can give it the object to cal
You can use the Delegate.CreateDelegate
method to create a strongly-typed delegate for a MethodInfo
.
If you don't know the method's signature at compile-time, you can either create a Func<...>
using Reflection, or create a lambda expression that invokes the MethodInfo
:
MethodInfo methodInfo = ...;
object thisObj = ...;
Func
(This is called currying)
Note that this will still have the performance hit from reflection every time the delegate is called, unlike Delegate.CreateDelegate
.