问题描述
我需要浏览某些类的所有已发布属性。
未列出类型为具有固定值的枚举的属性。
I need to browse all published properties of some classes.Properties where the type is an enumeration with fixed values are not listed.
请参见以下示例:
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;
我需要固定值,因为这些属性存储在数据库中,并且我需要确保分配的值将始终保持相同,无论下一个版本中的Delphi编译器选择如何,并防止在枚举列表中插入任何将来的值。
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.
我尝试使用两个新的Delphi 2010 RTTI (带有.GetDeclaredProperties)和旧 RTTI(带有GetPropInfos):除上述类型的属性外,找到所有属性。
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.
尝试使用和不使用各种RTTI指令,而无需进行更改。
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)?
使用Delphi2010 Ent + Update5
Using Delphi2010 Ent+Update5
下面的答案提供了解决方法:枚举的第一个值必须设置为0而不是1,并且值是连续的。经过测试和有效的解决方案。
谢谢,
推荐答案
Barry Kelly :
Barry Kelly says:
我考虑为以下对象实现tkDiscontiguousEnumeration(或可能命名更好的成员) Delphi 2010,但考虑到它们的相对稀缺性和枚举的困难,收益似乎很小-您如何有效地编码范围?有些编码在某些情况下更好,而在另一些情况下则更糟。
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.
这篇关于固定枚举未返回RTTI属性:这是一个错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!