开发者

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

I'm creating a generic list class that has a member of type Array(Array of ). The problem is the class destruction,because the class is supposed to be used for types from byte to types inheriting TObject.

Specifically:

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 do I check if T is TObject so I can free every member if the typeidenitifier is a class,for example?


You can get the type info of your type T with the TypeInfo compiler magic function. It returns a pointer which you can cast to PTypeInfo, which is declared in the TypInfo unit. So:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜