Doing a comparison simply means comparing the two
MethodInfo
signatures. The signature for a delegate is defined by the
Invoke
function. For each
MethodInfo
you can just compare the method signatures:
public delegate int CompareDelegate(string param1, double param2);
..
public bool IsMatch(MethodInfo methodInfo)
{
var compare = typeof(CompareDelegate).GetMethod("Invoke");
if (compare.ReturnType != methodInfo.ReturnType)
return false;
var methodParams = methodInfo.GetParameters().Select(p => p.ParameterType);
var compareParams = compare.GetParameters().Select(p => p.ParameterType);
return methodParams.SequenceEqual(compareParams);
}
That should give you a foundation for checking compatibility, however if you really want to ensure that the method is going to be compatible then you'd also want to compare for things like generic type parameters as well.
Alternatively you can use the
MethodInfo.CreateDelegate
and catch the exception, but doing that for every method is a bad idea.