Click here to Skip to main content
15,611,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a small utility I am working on, I want to do something similar to a plug-in, but not the usual. Usually, you define an Interface specifying the methods and properties which a compliant plug-in must contain. The plug-in class must contain those methods and properties.
But, what I am doing is simpler -- I have defined a delegate and I want to locate methods which match that delegate definition -- regardless of name. I can iterate the methods in a class, and -- if it conforms to that delegate -- I can make a delegate instance. That's not difficult.
The problem comes in not knowing how to test: Does the method described by this MethodInfo conform to the definition of this delegate?

Is there already such a test built into .net? I haven't seen one and I'd prefer not to have to implement one.

What I have tried:

At this time, I have put the code in a try/catch which ignores the exception, which works, but it's not ideal. Shouldn't I be able to use the test within CreateDelegate which results in the ArgumentException ?
Updated 19-Jul-21 22:26pm
OriginalGriff 20-Jul-21 1:45am    
Doesn't that kinda depend on your definition of "conform"?
Yes, you can extract method with matching signatures, but that doesn't mean they conform to the requirements of the plug in in terms of function.
PIEBALDconsult 20-Jul-21 9:42am    
Besides, to avoid confusing the issue I left out the detail that the method also has to be decorated with a custom attribute which states that the method is to be used in that way. The method needs to have the attribute (easy peasy, done) _and_ it has to match the delegate.

1 solution

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");

  // Compare the return type
  if (compare.ReturnType != methodInfo.ReturnType)
    return false;

  var methodParams = methodInfo.GetParameters().Select(p => p.ParameterType);
  var compareParams = compare.GetParameters().Select(p => p.ParameterType);

  // Compare the parameters
  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.
Share this answer
PIEBALDconsult 20-Jul-21 9:50am    
Right, but I was hoping that there was already such a test built in, which could compare _any_ MethodInfo against _any_ delegate definition.
The runtime already has to perform such a test.
Chris Copeland 20-Jul-21 10:13am    
I don't think there is, in the source code Delegate the actual creation is delegated to the unmanaged code to handle. This is probably where the compatibility is checked: Delegate.cs

I don't think there's anything currently built into C# to do this: StackOverflow of the same question, but then I don't see the problem with banging together a quick extension method to implement it.
PIEBALDconsult 20-Jul-21 11:46am    
True, creating an Extension Method to perform it should be no big deal.
And yet, and yet...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900