Jump to content
David Schwartz

custom attributes question

Recommended Posts

say you have a class like this:


TMyClass = class

  [attr1( 'abc' )]
  FField1 : string;
  . . .
  [attr2( 'abc' )]
  property Field1 : string read FField1 write FField1;
  . . .

Are attr1 and attr2 basically the same? (And yes, I get that they're different attribute classes. That's not what I'm referring to.)


What I mean is, does it make a difference whether you put the attribute on the data field (ie, attr1) or on the property (ie, attr2)? Or are they equivalent?


So ... is the property Field1 able to see attr1, or attr2, or both?

Edited by David Schwartz

Share this post

Link to post

They are different, but you can parse and look for the field or for the property for yourself. You can also put as many attributes to your field/property as you want.


Edited by Attila Kovacs

Share this post

Link to post

TRttiType.GetProperties.GetAttributes vs TRttiType.GetFields.GetAttributes


Personally, I've avoided setting props on fields and only set them on properties. 

There are parts of the RTL where the opposite applies - f.x. for REST.JsonReflect, which checks the fields and not the properties.

Share this post

Link to post

actually, come to think of it, I guess it's also possible to set attributes on the read and write things, and they can be either fields or methods.


I was wondering about the transitivity of the lookup with fields, but with methods it's going to be different. So that pretty much answers it.

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now