![]() |
Delphi-Version: 2010
Rtti tkBoolean und tkDate nachbilden
Hallo zusammen,
ich spiele gerade etwas mit der neuen RTTI rum und stoße da gerade auf ein kleines Problem. Ich wollte die Propertys einer Klasse mit Attributen "dekorieren" und diese dann über entsprechende Methoden in eine Datenbank serialisieren. Soweit alles kein Problem, bis auf den Fakt, dass er beim Auslesen der Propertys z.Bsp. ein BOOLEAN Property als tkInteger abhandelt. Wenn ich jetzt versuche eine Query mit Parametern zu bestücken um einen Datensatz zu speichern oder zu updaten sind diese Werte nicht zuweisungskompatibel. Habe ich also eine Chance herauszubekommen von welchem realen Typ ein Property ist ?
Delphi-Quellcode:
procedure TKultur.WritePropValues (const AKultur: TKultur; AQuery : TnxQuery);
var Context: TRttiContext; RttiType: TRttiType; PropInfo: TRttiProperty; F: Boolean; Attr: TCustomAttribute; Value: TValue; i : integer; begin if not Assigned(AKultur) then Exit; Context := TRttiContext.Create; try RttiType := Context.GetType(AKultur.ClassType); if Assigned(RttiType) then begin for PropInfo in RttiType.GetProperties do begin F := False; for Attr in PropInfo.GetAttributes do begin if Attr is DBField then F := True; end; if F then begin Value := PropInfo.GetValue(AKultur); case Value.Kind of tkInt64, tkInteger: AQuery.ParamByName (PropInfo.Name).asInteger := Value.AsInteger; tkFloat: AQuery.ParamByName (PropInfo.Name).asFloat := Value.AsExtended; tkChar, tkWChar, tkLString, tkWString, tkUString, tkString: AQuery.ParamByName (PropInfo.Name).asString := Value.AsString; end; end; end; end; finally Context.Free; end; |
AW: Rtti tkBoolean und tkDate nachbilden
In deiner Überschrift frägst du nach TDate / TTime / TDateTime. Diese 3 sind alle Double, können also von Delphi nicht als eines der 3 identifiziert werden (wie auch). Da du aber Attribute verwenden kannst, kannst du ja noch ein weiteres Attribut einführen, welches dir sagt, dass du den Integer/Double als Boolean/TDateTime zu verwenden hast.
Bernhard |
AW: Rtti tkBoolean und tkDate nachbilden
|
AW: Rtti tkBoolean und tkDate nachbilden
Ich danke, euch!!
@rollstuhlfahrer: ja das war mir klar, ich kam bloß nicht auf die Idee mit dem anderen Attribut. Ich probier's aus. @SirRufo: kannte ich noch nicht, schaue ich mir auch an. |
AW: Rtti tkBoolean und tkDate nachbilden
In Delphi XE ist Boolean übrigens nicht tkInteger sondern tkEnumeration.
Kleines Codebeispiel (ging übrigens schon vor der neuen Rtti, genauere Typenbestimmungen von Properties zu machen mit dieser Methode):
Delphi-Quellcode:
type
TFoo = class(TPersistent) private FActive: Boolean; FLastUpdate: TDateTime; public property Active: Boolean read FActive write FActive; property LastUpdate: TDateTime read FLastUpdate write FLastUpdate; end; procedure TForm1.Button1Click(Sender: TObject); var c: TRttiContext; p: TRttiProperty; begin for p in c.GetType(TFoo).GetProperties do begin case p.PropertyType.TypeKind of tkEnumeration: begin if p.PropertyType.Handle = TypeInfo(Boolean) then begin ShowMessage('Boolean found!'); end; end; tkFloat: begin if p.PropertyType.Handle = TypeInfo(TDate) then ShowMessage('TDate found') else if p.PropertyType.Handle = TypeInfo(TTime) then ShowMessage('TTime found') else if p.PropertyType.Handle = TypeInfo(TDateTime) then ShowMessage('TDateTime found'); end; end; end; end; |
AW: Rtti tkBoolean und tkDate nachbilden
Hallo Stevie,
also das hat's gebracht!! Ich hatte es schon mit zwei weiteren Attributen umgesetzt, fand es aber hässlich. Die neue Version über TypeInfo ist bedeutend besser! Danke dafür. BTW.: Boolean ist auch unter D2010 tkEnumeration cu cg |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:39 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-2025 by Thomas Breitkreuz