Search code examples
delphirtti

RTTI properties not returned for fixed enumerations: is it a bug?


I need to browse all published properties of some classes. Properties where the type is an enumeration with fixed values are not listed.

See example below:

TMyEnum = (meBlue, meRed, meGreen);
TMyEnumWithVals = (mevBlue=1, mevRed=2, mevGreen=3);
TMyClass =
...
published
  property Color: TMyEnum read FColor write SetColor; // This one is found
  property ColorVal: TMyEnumWithVals read FColorVal write SetColorVal; // This one is never found
end;

I need fixed values because these properties are stored in a database and I need to ensure that allocated values will always be the same, regardless of Delphi compiler choices in next versions, and prevent any misplaced insert of future values in the enumeration list.

I tried with both new Delphi 2010 RTTI (with .GetDeclaredProperties) and "old" RTTI (with GetPropInfos): all properties are found except the above type of property.

The same behaviour is seen on all classes. I also reproduced this in a sample project.

Tried with and without various RTTI directives without change.

Is this a bug, a known limitation? Is there a workaround (except removing fixed values of the enumeration)?

Using Delphi2010 Ent+Update5

[Edit] The answer below provides the workaround: The first value of the enumeration must be set to 0 instead of 1, and values contiguous. Tested and working solution.

Thanks,


Solution

  • Barry Kelly says:

    Discontiguous enumerations and enumerations which don't start at zero don't have typeinfo. For typeinfo to be implemented, it would need to be in a different format from the existing tkEnumeration, owing to backward compatibility issues.

    I considered implementing a tkDiscontiguousEnumeration (or possibly better named member) for Delphi 2010, but the benefit seemed small considering their relative scarcity and the difficulties in enumeration - how do you encode the ranges efficiently? Some encodings are better for some scenarios, worse for others.

    So it's not a bug, but a known limitation.