Is there a way to see if a method is overridable from its MethodInfo object? I was hoping that there was an IsOverridable property but no luck.
I am getting the MethodInfo from an object via:
object.gettype.getmethods
Printable View
Is there a way to see if a method is overridable from its MethodInfo object? I was hoping that there was an IsOverridable property but no luck.
I am getting the MethodInfo from an object via:
object.gettype.getmethods
Did some digging and came up with this. You have to check the IsVirtual and IsFinal property. Overidable methods are declared as virtual in C#.
link for ms-helpQuote:
A virtual member may reference instance data in a class and must be referenced through an instance of the class.
To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. For a method to be overridable, IsVirtual must be true and IsFinal must be false. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as virtual (Overridable in Visual Basic); therefore, the compiler marks the method virtual (Overridable in Visual Basic) final. So there are cases where a method is marked as virtual (Overridable in Visual Basic) but is still not overridable.
To establish with certainty whether a method is overridable, use code such as this: if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If IsVirtual is false or IsFinal is true, then the method cannot be overridden.
http://ms-help://MS.NETFrameworkSDK/...rtualtopic.htm
Thank you very much. I don't know how you found that but thanks.
I actually fell victim to the IsVirtual trick but when it didn't work with that alone I thought I was on the wrong track.
No prob :)