Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: RTTI: Wie kann ich prüfen, ob ein Feld vom Typ einer Klasse ist?

  Alt 24. Apr 2014, 12:11
PS: Das in eine Helfer-Klasse zu packen war jetzt kein Ding, aber mir kommt es trotzdem komisch vor. Gibt es da nichts fertiges in der RTTI?


Delphi-Quellcode:
unit RttiTypeHelper;

interface

uses System.Rtti;

type
   TRttiTypeHelper = class helper for TRttiType
      private const
         Para_inheritsFromType_includeCurrent = True;

      public
         function inheritsFromType(
            const    baseType:    TRttiType;
            const includeCurrent: Boolean = Para_inheritsFromType_includeCurrent
         ): Boolean; overload;

         function inheritsFromType(
            const    baseClass:    TClass;
            const includeCurrent: Boolean = Para_inheritsFromType_includeCurrent
         ): boolean; overload;
   end;

implementation

function TRttiTypeHelper.inheritsFromType(
   const    baseType:    TRttiType;
   const includeCurrent: Boolean = Para_inheritsFromType_includeCurrent
): Boolean;
var
   currentClassType: TRttiType;
begin
   Result := False;

   if not includeCurrent then
      currentClassType := self.baseType
   else
      currentClassType := self;

   while (not Result) and Assigned(currentClassType) do begin
         if currentClassType = baseType then
            Result := True;
         currentClassType := currentClassType.baseType;
      end;
end;

function TRttiTypeHelper.inheritsFromType(
   const    baseClass:    TClass;
   const includeCurrent: Boolean = Para_inheritsFromType_includeCurrent
): Boolean;
var
   rttiContext: TRttiContext;
begin
   rttiContext := TRttiContext.Create();
   Result := inheritsFromType( rttiContext.GetType(baseClass), includeCurrent);
end;

end.
  Mit Zitat antworten Zitat