Delphi 2010 RTTI : Explore Enumerations

后端 未结 6 2002
挽巷
挽巷 2021-02-05 16:04

Considering such an enumeration :

type
  TTypeOfData = (
    [XmlName(\'ABC\')] todABC,
    [XmlName(\'DEF\')] todDEF,  
    [XmlName(\'GHI\')] todGHI
  );
         


        
6条回答
  •  忘掉有多难
    2021-02-05 16:33

    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:

    {$SCOPEDENUMS ON}
    type
      TTypeOfData = (ABC,DEF,GHI);
    

    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.

提交回复
热议问题