In TMyList<T>, a generic list similar to TList<T> but simpler, I am trying to free any objects that may be stored in it if OwnsObjects is true.
I can check if the item is tkClass by using GetTypeKind(T), but that does not get me anywhere because I can't seem to be able to typecast T to TObject to free it.
procedure TMyList<T>.Clear;
var
Index1: Integer;
begin
if FOwnsObjects then begin
Index1 := 0;
while Index1 < FCount do begin
case GetTypeKind(FList^[Index1]) of
tkClass: begin
if TObject(FList^[Index1]) <> nil then begin
TObject(FList^[Index1]).Free;
TObject(FList^[Index1]) := nil;
end;
end;
end;
Inc(Index1);
end;
end;
{more code..}
end;
Is there no way to do this?