Delphi 2010 RTTI : Explore Enumerations

后端 未结 6 1925
挽巷
挽巷 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:36

    Ok I think I have found a better solution. I declare a new attribute type, e.g.:

    TEnumAttribute = class (TCustomAttribute)
      private 
        FCaption : string;
      public
        constructor Create (const Caption : string);
        property Caption : string read FCaption write FCaption;
    end;
    

    Now I add attributes to my enumeration:

    [TEnumAttribute ('Normal')]
    [TEnumAttribute ('High')]
    TExampleEnum = (eeNormal,eeHigh);
    

    Now it is easy to access the attributes by its ordinal:

    RttiType := RttiContext.FindType ('ExampleUnit.TExampleEnum');
    RttiAttributes := Rttitype.GetAttributes;
    Test := TEnumAttributes(RttiAttributes[index]).Caption;
    

提交回复
热议问题