Find out if a given type inherits from another type

Hello,

the typeOf Operator is true if the type of the tested variable is of or inherits from the tested type.

If i have a given type, is there a Operator or function to test if that given type is or inherits from another type or must i use an own function like the following to test this.

Markus

...
Debug.Assert(clsProc_Service.Type1_Is_Type2_Or_IsBaseType_Of_Type2 _
                             (GetType(clsBase), GivenType), "Assertion failed...")
...


Public Shared Function Type1_Is_Type2_Or_IsBaseType_Of_Type2 _
                       (ByVal pobjType1 As Type, _
                        ByVal pobjType2 As Type) As Boolean

  If pobjType1 Is pobjType2 Then
    Return True

  Else
    Dim objType2_BaseType As Type = pobjType2.BaseType

    Do While objType2_BaseType IsNot Nothing
      If pobjType1 Is objType2_BaseType Then
        Return True

      Else
        objType2_BaseType = objType2_BaseType.BaseType

      End If
    Loop
    Return False

  End If
End Function

 

 

 



Answer this question

Find out if a given type inherits from another type

  • TO

    You can use the IsAssignableFrom method on the Type class.

  • Find out if a given type inherits from another type