1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | TypInfo; type TDayAndMonth = (JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,NOV,OCT,DEC); function GetEnumName( const V; TypeInfo: PTypeInfo): string ; overload; var p : PAnsiChar ; i, nVal : Integer ; begin if (TypeInfo . Kind<>tkEnumeration) then raise Exception . Create( 'Not an Enumeration' ); p := @TypeInfo . Name; with PTypeData(p + Integer (p^) + 1 )^ do begin case OrdType of otSByte, otUByte: nVal := Byte (V); otSWord, otUWord: nVal := Word (V); else nVal := Integer (V); end ; if ((nVal<MinValue) or (nVal>MaxValue)) then raise Exception . Create( 'Not in range' ); p := @NameList; for i:=MinValue to nVal- 1 do Inc(p, Integer ( PShortString (p)^[ 0 ])+ 1 ); end ; Result := PShortString (p)^; end ; function GetEnumName(Enum: Integer ; TypeInfo: PTypeInfo): string ; overload; var p : PAnsiChar ; i : Integer ; begin if (TypeInfo . Kind<>tkEnumeration) then raise Exception . Create( 'Not an Enumeration' ); p := @TypeInfo . Name; with PTypeData(p + Integer (p^) + 1 )^ do begin if ((Enum<MinValue) or (Enum>MaxValue)) then raise Exception . Create( 'Not in range' ); p := @NameList; for i:=MinValue to Enum- 1 do Inc(p, Integer ( PShortString (p)^[ 0 ])+ 1 ); end ; Result := PShortString (p)^; end ; procedure TForm1 . Button1Click(Sender: TObject); var foo : TDayAndMonth; begin Memo1 . Lines . Add( GetEnumName(Ord(SEP), TypeInfo(TDayAndMonth)) ); foo := FEB; Memo1 . Lines . Add( GetEnumName(foo, TypeInfo(TDayAndMonth)) ); end ; |
联系客服