打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
枚举类型变量的值如何输出
一个利用 rtti 的例子(d7):
Delphi/Pascal code?
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;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
string如何转化为TFieldType
JSON 之 SuperObject(13): 关于 SO 与 SA 函数
枚举可以这样遍历
自定义函数
DELPHI RTTI浅析
enum
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服