Reputation: 88
How can I determine whether a Delphi < T > is of a specific class and not for example record? I want to release an element of a list, if it is a class when cleaning.
procedure TTableData<T>.Delete(Index: Integer);
begin
if Items[Index] is TClass then TClass(Items[Index]).Free;
inherited Delete(Index);
end;
Upvotes: 1
Views: 300
Reputation: 612954
You can use RTTI, perhaps like this:
uses
System.TypInfo;
....
procedure TTableData<T>.Delete(Index: Integer);
var
item: T;
begin
if PTypeInfo(TypeInfo(T)).Kind = tkClass then
begin
item := Items[index];
TObject((@item)^).Free;
end;
inherited Delete(Index);
end;
Upvotes: 2