如何获取枚举类型到字符串列表? [英] How to get enumeration type into stringlist?

查看:79
本文介绍了如何获取枚举类型到字符串列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一行代码:

ShowMessage(GetEnumName(TypeInfo(TAlign), 1));

返回"alTop".

当我要使用字符串变量:"TAlign"而不是TAlign时,如何将枚举类型的所有值都放入字符串列表?像这样:

How can I get all values of enumerated type into stringlist, when I want to use string variable: 'TAlign' instead of TAlign? Something like:

ShowMessage(GetEnumName(TypeInfo('TAlign'), 1));

感谢

推荐答案

要能够使用字符串变量,您需要在某种查找表中向该字符串注册TypeInfo,然后对其进行查找.

To be able to use a string variable, you'd need to register the TypeInfo with the string in some sort of lookup table, and then look it up.

要获取列表中所有枚举的类型名称,可以执行以下操作:

To get all the enumerated type names in your list, you can do something like this:

procedure LoadAllEnumValuesIntoStringList(enum: PTypeInfo; list: TStringList);
var
   data: PTypeData;
   i: integer;
begin
   list.clear;
   data := GetTypeData(GetTypeData(enum)^.BaseType^);
   for i := 0 to data.MaxValue do
      list.add(GetEnumName(enum, i));
end;

这篇关于如何获取枚举类型到字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆