Results 1 to 4 of 4

Thread: How to tell if a method is Overridable from the MethodInfo

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    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

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  3. #3

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
  •  



Click Here to Expand Forum to Full Width