In C# code, how do I check if a given method can be represented by a particular delegate type?
I first tried something, based on my Type knowledge, along the lines of:
// The delegate to test against.
void TargetDelegate(string msg);
// and...
var methodInfo = Type.GetMethod(..); // obtain the MethodInfo instance.
// try to test it
typeof(TargetDelegate).IsAssignableFrom(methodInfo.GetType());
but that deals with only Types and not methods - it will always be false.
My inclination is to believe the answer lies in the Delegate
Type, but I'm just wandering around the FCL at this point. Any help would be appreciated.
I'd try:
Delegate.CreateDelegate(typeof(TargetDelegate), methodInfo, false) != null
This will try to create the delegate and return null on failure. If it return null, it should mean that the delegate was not able to be created. If it returns anything else, the delegate must be OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With