[fpc-pascal] Usage of FieldAddress
Sven Barth
pascaldragon at googlemail.com
Mon Apr 5 09:17:20 CEST 2021
Ryan Joseph via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mo., 5. Apr. 2021, 05:58:
>
>
> > On Apr 4, 2021, at 2:36 PM, Sven Barth via fpc-pascal <
> fpc-pascal at lists.freepascal.org> wrote:
> >
> > The RTTI streaming relies on *published properties* (and published
> methods for the event handlers).
>
> Sorry Sven, I'm not understand what I'm doing wrong. From original example
> "scale" is a published property right? So FieldAddress only works on
> published fields, which are not supported unless they are
> classes/interfaces.
>
> type
> TSomething = class(TPersistent)
> private
> m_scale: integer;
> published
> property scale: integer read m_scale write m_scale;
> end;
>
> I have used published properties like this for JSON streaming, i.e.:
>
> type
> TVectorObject = class(TPersistent)
> private
> components: array[0..2] of integer;
> published
> property x: integer read components[0] write components[0];
> property y: integer read components[1] write components[1];
> property z: integer read components[2] write components[2];
> end;
>
> which works just fine so I assume I can use the RTTI functions, just not
> FieldAddress.
>
Correct. You need to use GetOrdProp and similar from the TypInfo unit or
the object oriented variant from the Rtti unit.
Note: you don't need to inherit from TPersistent if you simply want to
manually access such properties, setting $M+ before the type (and resetting
it afterwards) is enough to enable the published section.
Regards,
Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210405/7caf1b34/attachment.htm>
More information about the fpc-pascal
mailing list