![]() |
Rtti und Attribute in Delphi 2010
Hallo,
bin gerade dabei mir das Beispiel von ![]() Und zwar bei
Delphi-Quellcode:
MyAttribute = class(TCustomAttribute)
private FName: string; FAge: Integer; public constructor Create(const Name : string; Age : Integer); property Name : string read FName write FName; property Age : Integer read FAge write FAge; end;
Delphi-Quellcode:
Was passiert bei [MyAttribute('Malcolm', 39)]? Ist es richtig das in der Methode MyProc ein neues Objekt der Klasse MyAttribute mit den Inhalten ('Malcolm', 39) erzeugt wird?
TMyClass = class
public [MyAttribute('Malcolm', 39)] procedure MyProc(const s : string); [MyAttribute('Julie', 37)] procedure MyOtherProc; end; Das wäre dann gleich mit:
Delphi-Quellcode:
Nur wie erfolgt der Zugriff auf das mittels [MyAttribute('Malcolm', 39)] erzeugte Objekt? Kann man sich da auch so etwas wie var a : MyAttribute anlegen, um nicht jeden Zugriff über TRttiContext durchzuführen?
procedure MyProc (...)
var a : MyAttribute begin a := MyAttribute.create('Malcolm', 39); end; |
AW: Rtti und Attribute in Delphi 2010
Attribute sind Teil der RTTI, von daher musst du sie auch darüber auslesen.
Delphi-Quellcode:
var
ctx: TRttiContext; t: TRttiType; m: TRttiMethod; a: TCustomAttribute; begin t := ctx.GetType(TMyClass); m := t.GetMethod('MyProc'); for a in m.GetAttributes do begin if a is MyAttribute then begin ShowMessageFmt('Name: %s, age: %d', [MyAttribute(a).Name, MyAttribute(a).Age]); end; end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:47 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