Introspection Help

I’m trying to port this conditional from Java:

if (this.getClass().isInstance(someObject) || someObject().isInstance(this)) {
 // Some code
}

In essence, I need determine if Self is a descendant of someObject's type or if someObject's type is a descendant of Self's type.

Is this possible with introspection? If so, can anyone suggest how?

Thanks,

OK, I think this is the answer…

Var selfTI As Introspection.TypeInfo = Introspection.GetType(Self)
Var someObjectTI As Introspection.TypeInfo = Introspection.GetType(someObject)

If selfTI.FullName = someObjectTI Or selfTI.IsSubclassOf(someObjectTI) Or someObjectTI(selfTI) Then
  Return True
Else
  Return False
End If

I don’t think that would compile. IsSubclassOf should be all you need.

1 Like

As always you have the eyes of a hawk. This compiles:

Var selfTI As Introspection.TypeInfo = Introspection.GetType(Self)
Var someObjectTI As Introspection.TypeInfo = Introspection.GetType(someObject)

If selfTI.FullName = someObjectTI.FullName Or selfTI.IsSubclassOf(someObjectTI) Or someObjectTI(selfTI) Then
  Return True
Else
  Return False
End If

I thought IsSubclassOf would only report True if an instance was a subclass of another, not the same class?

You are correct @Kem_Tekinay:

// Class2 is a subclass of Class1
Var c1 As New Class1
Var c11 As New Class1
Var c2 As New Class2

Var ti1 As Introspection.TypeInfo = Introspection.GetType(c1)
Var ti11 As Introspection.TypeInfo = Introspection.GetType(c11)
Var ti2 As Introspection.TypeInfo = Introspection.GetType(c2)

Var b1 As Boolean = ti1.IsSubclassOf(ti1)  // True
Var b2 As Boolean = ti1.IsSubclassOf(ti11) // True
Var b3 As Boolean = ti1.IsSubclassOf(ti2)  // False
Var b4 As Boolean = ti2.IsSubclassOf(ti1)  // True

Break
1 Like

I should have bumper stickers made up. :slight_smile:

FYI, at least for your test, you didn’t have to instantiate an object to get the TypeInfo. This would have worked too:

var ti1 as Introspection.TypeInfo = GetTypeInfo( Class1 )
var ti11 as Introspection.TypeInfo = GetTypeInfo( Class1 )
var ti2 as Introspection.TypeInfo = GetTypeInfo( Class2 )
1 Like