Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object inherits from generic class

I have a generic list class:

TMyObjectlist<T: TMyObject> = class(TObjectList<T>);

and a derived list class:

TMyDerivedObjectList = class(TMyObjectList<TMyDerivedObject>);

I want to check if an instance MyList of TMyDerivedObjectList inherits from TMyObjectList, however:

MyList.InheritsFrom(TMyObjectlist<TMyObject>)

returns False.

It turns out that MyList.Classparent is of type TMyObjectList<TMyDerivedObject>.

Does anybody knows how to check the InheritsFrom in this case?

like image 930
Birger Avatar asked Feb 27 '23 19:02

Birger


1 Answers

Just draw up the inheritance scheme for both list objects and you'll clearly see why the InheritsFrom doesn't work. In Generics.Collections we have:

TEnumerable<T> = class abstract;
TList<T> = class(TEnumerable<T>);
TObjectList<T> = class(TList<T>);

In your example we have:

TMyObject = class;
TMyDerivedObject = class(TMyObject);

So we get those two inheritance trees:

TObject
|
TEnumerable<TMyDerivedObject>
|
TList<TMyDerivedObject>
|
TObjectList<TMyDerivedObject>

and then we have:

TObject
|
TEnumerable<TMyObject>
|
TList<TMyObject>
|
TObjectList<TMyObject>

As you can see, the only common ancestor for both list types is TObject!

like image 157
Cosmin Prund Avatar answered Mar 10 '23 00:03

Cosmin Prund