According to this answer, there could also be an easy way to check if the virtual method has been overridden so as not to know the exact derived or base type using the test for MethodAttributes.NewSlot :
public static bool HasOverride(this MethodInfo method) { return (method.Attributes & MethodAttributes.Virtual) != 0 && (method.Attributes & MethodAttributes.NewSlot) == 0; }
Together with another extension method
private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; public static bool HasOverride(this Type type, string name, params Type[] argTypes) { MethodInfo method = type.GetMethod(name, Flags, null, CallingConventions.HasThis, argTypes, new ParameterModifier[0]); return method != null && method.HasOverride(); }
you can just call
bool hasOverride = GetType().HasOverride(nameof(MyMethod), typeof(Param1Type), typeof(Param2Type), ...);
to check if MyMethod is MyMethod in a derived class.
As far as I tested this, it worked fine (on my machine β’).
buygrush May 01, '16 at 22:12 2016-05-01 22:12
source share