Checking if argument `object [] args satisfies delegate instance?

I have the following method signature:

public static void InvokeInFuture(Delegate method, params object[] args) { // ... } 

The delegate and arguments are stored in the collection for later recall.

Is there a way to check if the array of arguments satisfies the requirements of the delegate without calling it?

Thanks.

EDIT: Thanks for implementing reflection, but I'm looking for a built-in way to do this. I don’t want to reinstall the wheel, the .NET Framework has already done this check, implemented inside Delegate.DynamicInvoke () somewhere, an implementation that handles all these crazy special cases that only Microsoft developers can think of, and passed Unit Testing and QA. Is there any way to use this inline implementation?

Thanks.

+6
c # invoke arguments delegates
source share
3 answers

You can use reflection to get the delegate method signature as follows.

 using System; using System.Reflection; bool ValidateDelegate(Delegate method, params object[] args) { ParameterInfo[] parameters = method.Method.GetParameters(); if (parameters.Length != args.Length) { return false; } for (int i = 0; i < parameters.Length; ++i) { if (parameters[i].ParameterType.IsValueType && args[i] == null || !parameters[i].ParameterType.IsAssignableFrom(args[i].GetType())) { return false; } } return true; } 
+6
source share

It looks like the framework basically does the above, with some logic to eliminate it, to make sure it keeps a list of already rated ones.

+1
source share

see "Checking MethodInfo versus delegate" Checking MethodInfo versus delegate

+1
source share

All Articles