In Delphi, given the following:
TFruit = class;
TFruitClass = class of TFruit;
TApple = class(TFruit);
TRedApple = class(TApple);
If I have a TFruitClass
variable, how can I find out if it inherits from TApple
? E.g. say I have
var
FruitClass: TFruitClass;
...
FruitClass := TRedApple;
How can I verify that FruitClass does indeed inherit from TApple
in this case? Using FruitClass is TApple
only works for class instances.
Use InheritsFrom:
if TApple.InheritsFrom(TFruit) then
...
You can also use
var
Fr: TFruitClass;
X: TObject;
begin
if X.InheritsFrom(TFruit) then
Fr := TFruitClass(X.ClassType);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With