|
-
Dec 4th, 2002, 07:45 PM
#1
How to tell if a method is Overridable from the MethodInfo
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
-
Dec 4th, 2002, 09:36 PM
#2
Frenzied Member
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#.
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.
link for ms-help
http://ms-help://MS.NETFrameworkSDK/...rtualtopic.htm
Dont gain the world and lose your soul
-
Dec 4th, 2002, 10:48 PM
#3
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.
-
Dec 4th, 2002, 11:46 PM
#4
Frenzied Member
No prob
Dont gain the world and lose your soul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|