If I have an Enum type like this:
TOAuthSubjectTypes = (
[MappingAttr('public')]
ostPublic,
[MappingAttr('pairwise')]
ostPairwise
);
And in a given moment I have a variable that holds ostPairwise
, is it possible to retrieve the attribute for the value ostPairwise
? Right now the workaround I'm applying is to set all attributes in order for the Enum type and reaching them through GetAttributes
and the Ordinal value of the enum type, but I wonder if there's a more idiomatic way.
Sorry if it's duplicated. I've found a lot of answers on others' languages tags, but nothing related on Delphi's.
Unfortunately, there is simply no way to do what you want, as there is no RTTI stored for individual enum members, only for the enum type itself.
Per this answer to Delphi 2010 RTTI : Explore Enumerations (which still holds true for Delphi 12):
Attributes associated with elements in enumerations are not currently stored in Win32 RTTI data in the executable. RTTI is already responsible for a fair increase in the size of executables, so some lines had to be drawn somewhere. Attributes in Delphi Win32 are supported on types, on fields of records, and fields, methods, their parameters, and properties of classes.
The attribute declarations don't cause errors because of backward compatibility with Delphi for .NET.
So, your workaround to put the attributes on the enum type and then index them by enum value is the best you can do.