Browse Source

* Add IsInstanceOf

Michaël Van Canneyt 1 year ago
parent
commit
a9ff6272f6
1 changed files with 14 additions and 0 deletions
  1. 14 0
      packages/rtl-objpas/src/inc/rtti.pp

+ 14 - 0
packages/rtl-objpas/src/inc/rtti.pp

@@ -229,6 +229,7 @@ type
     function GetArrayElement(AIndex: SizeInt): TValue;
     function GetArrayElement(AIndex: SizeInt): TValue;
     procedure SetArrayElement(AIndex: SizeInt; constref AValue: TValue);
     procedure SetArrayElement(AIndex: SizeInt; constref AValue: TValue);
     function IsType(aTypeInfo: PTypeInfo): boolean; inline;
     function IsType(aTypeInfo: PTypeInfo): boolean; inline;
+    function IsInstanceOf(aClass : TClass): boolean; inline;
     function TryCast(aTypeInfo: PTypeInfo; out aResult: TValue; const aEmptyAsAnyType: Boolean = True): Boolean;
     function TryCast(aTypeInfo: PTypeInfo; out aResult: TValue; const aEmptyAsAnyType: Boolean = True): Boolean;
     function Cast(aTypeInfo: PTypeInfo; const aEmptyAsAnyType: Boolean = True): TValue; overload;
     function Cast(aTypeInfo: PTypeInfo; const aEmptyAsAnyType: Boolean = True): TValue; overload;
 {$ifndef NoGenericMethods}
 {$ifndef NoGenericMethods}
@@ -1844,6 +1845,19 @@ begin
   Result:=IsDateTimeType(TypeInfo);
   Result:=IsDateTimeType(TypeInfo);
 end;
 end;
 
 
+function TValue.IsInstanceOf(aClass : TClass): boolean; 
+
+var
+  Obj : TObject;
+
+begin
+  Result:=IsObject;
+  if not Result then
+    exit;
+  Obj:=AsObject;
+  Result:=Assigned(Obj) and Obj.InheritsFrom(aClass);
+end;
+
 {$ifndef NoGenericMethods}
 {$ifndef NoGenericMethods}
 generic function TValue.IsType<T>:Boolean;
 generic function TValue.IsType<T>:Boolean;
 begin
 begin