Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- uses
- Classes, Rtti,
- SysUtils;
- type
- AutoRegisterAttribute = class(TCustomAttribute);
- TMyStuff = class(TPersistent)
- procedure DoStuff; virtual;
- end;
- [AutoRegister]
- TMyAwesomeStuff = class(TMyStuff)
- procedure DoStuff; override;
- end;
- procedure ProcessAutoRegisterAttributes;
- var
- ctx: TRttiContext;
- typ: TRttiType;
- attr: TCustomAttribute;
- cls: TClass;
- begin
- for typ in ctx.GetTypes do
- if typ.IsInstance then
- for attr in typ.GetAttributes do
- if attr is AutoRegisterAttribute then
- begin
- cls := TRttiInstanceType(typ).MetaclassType;
- if cls.InheritsFrom(TPersistent) then
- RegisterClass(TPersistentClass(cls));
- Break;
- end;
- end;
- { TMyStuff }
- procedure TMyStuff.DoStuff;
- begin
- end;
- { TMyAwesomeStuff }
- procedure TMyAwesomeStuff.DoStuff;
- begin
- inherited;
- end;
- procedure UseStuff(stuff: TMyStuff);
- begin
- stuff.DoStuff;
- end;
- var
- cls: TPersistentClass;
- begin
- ProcessAutoRegisterAttributes;
- cls := FindClass('TMyAwesomeStuff');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement