I had expected to find an answer easily to this problem, but I didn’t.
I’d like to know if it is possible to determine whether a method has the keyword ‘override’ attributed to it, given its instance of MethodInfo.
I was thinking maybe the following would achieve that:
/// <summary> Returns whether the specified methodInfo is attributed with the keyword 'override'. </summary>
public static bool IsOverriding(this MethodInfo methodInfo)
{
if (methodInfo == null) throw new ArgumentNullException();
return methodInfo.DeclaringType != methodInfo.GetBaseDefinition().DeclaringType;
}
I’ve sucessfully tested some non-virtual, virtual and abstract examples, but I feel like I’m missing some scenarios, maybe with hiding or generics(although I can’t figure out how that would come into play).
Well, I don’t see how that would come into play either. Your code there does indeed determine whether a method is defined or overriden.
In the case of hiding, the declaring type is the one that hides the method via
new.In the case of generics, all methods are defined by the template class.