I have the MethodInfo
of a method on a class type that is part of an interface definition that that class implements.
How can I retrieve the matching MethodInfo
object of the method on the interface type that the class implements ?
I think i found the best way to do this :
var methodParameterTypes = classMethod.GetParameters().Select(p => p.ParameterType).ToArray();
MethodInfo interfaceMethodInfo = interfaceType.GetMethod(classMethod.Name, methodParameterTypes);
Looking up by name and parameters will fail for explicitly implemented interface methods. This code should handle that situation as well:
private static MethodInfo GetInterfaceMethod(Type implementingClass, Type implementedInterface, MethodInfo classMethod)
{
var map = implementingClass.GetInterfaceMap(implementedInterface);
var index = Array.IndexOf(map.TargetMethods, classMethod);
return map.InterfaceMethods[index];
}
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