Recent

Author Topic: [SOLVED] custom attributes in classes  (Read 761 times)

nomorelogic

  • Full Member
  • ***
  • Posts: 165
[SOLVED] custom attributes in classes
« on: February 20, 2020, 09:37:47 am »
hi all
I've some problems using custom attributes.


I declared a register procedure as
Code: Pascal  [Select][+][-]
  1. ...
  2.   { TBomt_HashServiceList }
  3.   TBomt_HashServiceList = specialize TFPGMap<string,TBomt_Service>;
  4. ...
  5.  
  6. var
  7.   ServiceList: TBomt_HashServiceList;
  8.  
  9. procedure Bomt_RegisterService(const AClassService: TClass);
  10. var Context : TRttiContext;
  11.     AType : TRttiType;
  12.     Attribute : TCustomAttribute;
  13. begin
  14.   Context := TRttiContext.Create;
  15.   try
  16.     AType := Context.GetType(typeinfo(AClassService));
  17.     for Attribute in  AType.GetAttributes do begin
  18.       if Attribute is TBomt_ServiceAttribute then begin
  19.          writeln('attr: ', TBomt_ServiceAttribute(Attribute).EndPoint);
  20.          ServiceList[TBomt_ServiceAttribute(Attribute).EndPoint]:= TBomt_Service(AClassService);
  21.       end;
  22.     end;
  23.   finally
  24.     Context.Free
  25.   end;
  26.  
  27. end;
  28.  

and in initialization section
Code: Pascal  [Select][+][-]
  1. initialization
  2.  
  3.    ServiceList:=TBomt_HashServiceList.Create;
  4.    Bomt_RegisterService(TBomt_Service);
  5.  

but my code is wrong as no attributes were detected.

I attached a complete test project to help.

Thanks in advance.
nomorelogic


Edit:

PS: note in test project there's a second unit with same initializazion code
executing I expect to have 2 registration
« Last Edit: February 21, 2020, 10:32:47 am by nomorelogic »

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: custom attributes in classes
« Reply #1 on: February 20, 2020, 09:44:24 am »
Did you read my example in the wiki? That should solve it. https://wiki.freepascal.org/Custom_Attributes#Complete_example
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: custom attributes in classes
« Reply #2 on: February 20, 2020, 09:57:08 am »
This is your mistake:

Code: Pascal  [Select][+][-]
  1. AType := Context.GetType(typeinfo(AClassService));

TypeInfo returns the type information of the literal type, so in this case TClass. What you want is this:

Code: Pascal  [Select][+][-]
  1. AType := Context.GetType(AClassService.ClassInfo);

or even simpler:

Code: Pascal  [Select][+][-]
  1. AType := Context.GetType(AClassService);

as GetType has an overload for TClass.

nomorelogic

  • Full Member
  • ***
  • Posts: 165
Re: custom attributes in classes
« Reply #3 on: February 20, 2020, 10:20:50 am »
Did you read my example in the wiki? That should solve it. https://wiki.freepascal.org/Custom_Attributes#Complete_example

Yes I used code in "complete example" to write my test project.
See "constructor TMyDateTimeClass.Create;"

nomorelogic

  • Full Member
  • ***
  • Posts: 165
Re: custom attributes in classes
« Reply #4 on: February 20, 2020, 10:27:48 am »
TypeInfo returns the type information of the literal type, so in this case TClass. What you want is this:

Thank you this fix my code!

nomorelogic

 

TinyPortal © 2005-2018