AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Attribute überschreiben

Ein Thema von Rainer Wolff · begonnen am 3. Aug 2015 · letzter Beitrag vom 5. Aug 2015
 
Der schöne Günther

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

AW: Attribute überschreiben

  Alt 3. Aug 2015, 15:33
Hätte man doch einfach mal ausprobieren können, oder?

Delphi-Quellcode:
program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses System.SysUtils, System.Rtti;

type
   NumericAttribute = class(System.TCustomAttribute)
      public var
         someNumber:   Integer;
      public
         constructor Create(const someNumber: Integer);
   end;

   [Numeric(42)]
   TBase = class(TObject)
      // Stub
   end;

   [Numeric(99)]
   TSub = class(TBase)
      // TStub
    end;

{ TNumericAttribute }

constructor NumericAttribute.Create(const someNumber: Integer);
begin
   inherited Create();
   self.someNumber := someNumber;
end;

procedure printNumericInfo(const ofType: TRttiType);
var
   attribute: TCustomAttribute;
   numeric: NumericAttribute;
begin
   for attribute in ofType.GetAttributes() do begin
      if not (attribute is NumericAttribute) then Continue;
         numeric := attribute as NumericAttribute;
         Write( ofType.ToString() );
         Write(' has a numeric value of ');
         WriteLn(numeric.someNumber);
   end;
end;

procedure justRttiThings();
var
   ctx: TRttiContext;
   base: TBase;
   sub: TSub;

begin
   ctx := TRttiContext.Create();
   base := TBase.Create();
   sub := TSub.Create();

   printNumericInfo( ctx.GetType(base.ClassType) );
   printNumericInfo( ctx.GetType(sub.ClassType) );
end;

begin
  try
   justRttiThings();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

  readln;
end.
ergibt bei mir
Code:
TBase has a numeric value of 42
TSub has a numeric value of 99

Oder habe ich das falsch verstanden?
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:18 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