How to check if TypeIdenitifier (T) is an object?

I am creating a generic list class that has a member of type Array (Array of). The problem is in the destruction of the class, since the class is supposed to be used for types from byte to types that inherit TObject.

In particular:

destructor Destroy;
var elem:T;
begin
  /*if(T is Tobject) then  //Check if T inherits TObject {Compiler error!}
    for elem in FData do TObject(elem).Free;*/    // do not know how to do it

  SetLength(FItems,0); //FItems : Array of T
  inherited Destroy;
end;

      

How can I check if T is a TObject so that I can free each member if the typeidenitifier is a class for example?

+2


a source to share


1 answer


You can get information about the type of your type T with the magic function of the TypeInfo compiler. It returns a pointer that you can use for PTypeInfo, which is declared in the TypInfo module. So:



if PTypeInfo(typeInfo(T)).Kind = tkClass then
  //do whatever

      

+2


a source







All Articles