![]() |
Delphi-Version: XE5
RTTI: Wie kann ich prüfen, ob ein Feld vom Typ einer Klasse ist?
Und ich dachte, ich könnte mit der RTTI umgehen.
Folgendes:
Delphi-Quellcode:
Ich möchte alle Felder in
type
TBaseClass = class(TObject); TSubClass = class(TBaseClass); TMyContainer = class first: TBaseClass; second: TSubClass; third: TObject; end;
Delphi-Quellcode:
finden, die zuweisungskompatibel zu
TMyContainer
Delphi-Quellcode:
sind. Also
TBaseClass
Delphi-Quellcode:
und
first
Delphi-Quellcode:
.
second
Wie tue ich das? Mir fällt nichts anderes ein, als nur strikt Typ mit Typ vergleichen zu können. Folgengender Code
Delphi-Quellcode:
ergibt
var
context: TRttiContext; fieldIterator: TRttiField; begin context := TRttiContext.Create(); for fieldIterator in context.GetType(TMyContainer).GetFields() do begin WriteLn( 'Feld gefunden: '+fieldIterator.Name.QuotedString() ); if fieldIterator.FieldType = context.GetType(TBaseClass) then WriteLn('Das Feld ist eine zu TBaseClass kompatible Instanz'); if Assigned(fieldIterator.FieldType.BaseType) then WriteLn('BaseType-Name: '+fieldIterator.FieldType.BaseType.Name); WriteLn(EmptyStr); end; ReadLn; end.
Code:
Man sieht:
Feld gefunden: 'first'
Das Feld ist eine zu TBaseClass kompatible Instanz BaseType-Name: TObject Feld gefunden: 'second' BaseType-Name: TBaseClass Feld gefunden: 'third'
Nur: Muss ich jetzt für jedes Feld die gesamte Vererbungshierarchie bis
Delphi-Quellcode:
hochkrabbeln um zu schauen, ob meine gewollte Basisklasse einmal vorkommt?
TObject
Frage: Gibt es in der RTTI nichts wie das normale
Delphi-Quellcode:
bzw. den
InheritsFrom(..)
Delphi-Quellcode:
-Operator?
is
|
AW: RTTI: Wie kann ich prüfen, ob ein Feld vom Typ einer Klasse ist?
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. |
AW: RTTI: Wie kann ich prüfen, ob ein Feld vom Typ einer Klasse ist?
Delphi-Quellcode:
if fieldIterator.FieldType.IsInstance then begin
if fieldIterator.FieldType.AsInstance.MetaclassType.InheritsFrom(TBaseClass) then begin WriteLn('Das Feld ist eine zu TBaseClass kompatible Instanz'); end; end; |
AW: RTTI: Wie kann ich prüfen, ob ein Feld vom Typ einer Klasse ist?
Genau das! :thumb:
Herzlichen Dank. Wesentlich kürzer :-D |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:59 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz