Delphi 2010 RTTI:探索枚举
考虑这样一个枚举:
type
TTypeOfData = (
[XmlName('ABC')] todABC,
[XmlName('DEF')] todDEF,
[XmlName('GHI')] todGHI
);
其中 XmlName 是一个自定义属性,用于定义此枚举成员的序列化字符串。
如何探索附加到该枚举的每个成员的属性?
Considering such an enumeration :
type
TTypeOfData = (
[XmlName('ABC')] todABC,
[XmlName('DEF')] todDEF,
[XmlName('GHI')] todGHI
);
Where XmlName is a custom attribute used to define the serialization string for members of this enumeration.
How can I explore the attributes attached to each member of this enumeration ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(7)
虽然巴里清楚地回答了您有关枚举元素属性的问题,但我将尝试另一个建议。从您的示例中,您按照 Delphi 中的传统做法为每个枚举元素添加“tod”前缀,因为枚举元素在范围内是全局的(即,如果除了 todABC 枚举元素之外,您在范围内还有一个标识符 todABC ,您可以获得一些奇怪的行为)。
从 D2007 开始,我们引入了“作用域枚举”的概念,启用该概念后,要求您使用枚举本身的标识符来限定枚举元素。例如:
将要求您将 ABC 元素引用为 TTypeOfData.ABC。这允许您使用无前缀的枚举元素标识符,并且不会冒冲突的风险,因为元素的“范围”仅限于枚举。启用 {$SCOPEDENUMS} 时声明的任何枚举都将以这种方式运行。
鉴于此,您现在可以安全地使用 RTTI 以您希望的格式获取实际的枚举元素名称。
While Barry clearly answered your question regarding the attributes on enum elements, I'll take a stab at another suggestion. From your example, you're prefixing each enum element with 'tod' as is traditional in Delphi because enum elements are global in scope (ie. if you had an identifier todABC in scope in addition to the todABC enum elements, you could get some odd behaviors).
Starting in D2007, we introduced the notion of "scoped enums" which, when enabled, require you to qualify the enum element with the identifier of the enum itself. For instance:
Will require you to refer to the ABC element as TTypeOfData.ABC. This allows you to use non-prefixed enum element identifiers and not run the risk of having conflicts since the elements are "scoped" to the enumeration. Any enum declared while {$SCOPEDENUMS} is enabled will behave in this manner.
Given that, you can now safely use the RTTI to get the actual enum element names in the format you wish.
与枚举中的元素关联的属性当前未存储在可执行文件的 Win32 RTTI 数据中。 RTTI 已经导致可执行文件大小的合理增加,因此必须在某处绘制一些线。 Delphi Win32 中的属性支持类型、记录字段以及类的字段、方法、它们的参数和属性。
由于与 Delphi for .NET 向后兼容,属性声明不会导致错误。
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.
以下是网络上 Delphi 2010 中 RTTI 的一个很好的概述:http://robstechcorner.blogspot.com/2009/09/so-what-is-rtti-rtti-is-acronym-for-run.html
你可以得到使用 TypInfo 单元中的“旧”RTTI 函数(GetEnumValue、GetEnumName)获取枚举值并返回序数。剪掉小写字母,你会得到与上面相同的结果,但它不那么灵活。
These is a good overview of RTTI in Delphi 2010 on the web: http://robstechcorner.blogspot.com/2009/09/so-what-is-rtti-rtti-is-acronym-for-run.html
You can get the enumeration values and back the ordinals using the "OLD" RTTI functions in the unit TypInfo (GetEnumValue, GetEnumName). And clip off the lowercase letters you get the same result as above but it is not as flexible.
好吧,我想我已经找到了更好的解决方案。我声明一个新的属性类型,例如:
现在我将属性添加到我的枚举中:
现在可以很容易地按序号访问属性:
Ok I think I have found a better solution. I declare a new attribute type, e.g.:
Now I add attributes to my enumeration:
Now it is easy to access the attributes by its ordinal:
对于那些对这个问题的实际解决方案感兴趣的人,我这样解决了:
后来,在序列化代码中,我使用 RTTI 来查找通常名为 AsString 的类函数,并使用属性 TValue 调用它:
For those who are interrested in a practical solution to that problem, I solved it that way :
And later, in the serialization code, I use RTTI to look for a class function conventionnaly named AsString and call it with the property TValue :
我在 const 部分使用字符串数组:
I use and array of string in the const section:
我使用我的自定义属性。
I use my custom attribute.