Delphi 将枚举转换为字符串列表的函数....?
Delphi Function to convert enum to string list....?
我想要一个接受枚举和 returns 字符串列表(或 name:value 对)的函数。一定有可能,Object Inspector 和 Code Complete 似乎可以做到这一点。伪代码看起来很简单,但细节让我难以理解....
function ParseEnum(e: Enum, S: TStringList): TStringList;
var
i: Integer;
begin
for i := 0 to length(enum) - 1 do
S.Add(GetEnumName(e, i));
Result := S;
end;
如果这是一个通用函数(所有 Enum):
如果 Enum 是 Continuity,如下代码是 OK(by rtti)
如果不是 Continuity 我现在找不到方法
type
TMyEnum = (s, b, c, d, e, f);
implementation
uses
System.Rtti, TypInfo;
procedure ParseEnum<T>(sl: TStrings);
var
rt: TRttiType;
rot: TRttiOrdinalType;
i: Integer;
begin
rt := TRttiContext.Create.GetType(TypeInfo(T));
rot := rt.AsOrdinal;
for i := rot.MinValue to rot.MaxValue do
sl.Add(GetEnumName(TypeInfo(T), i));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
ParseEnum<TMyEnum>(sl);
ShowMessage(sl.Text);
finally
sl.Free;
end;
end;
为什么sl作为参数但是结果:注意人们不要忘记释放
我想要一个接受枚举和 returns 字符串列表(或 name:value 对)的函数。一定有可能,Object Inspector 和 Code Complete 似乎可以做到这一点。伪代码看起来很简单,但细节让我难以理解....
function ParseEnum(e: Enum, S: TStringList): TStringList;
var
i: Integer;
begin
for i := 0 to length(enum) - 1 do
S.Add(GetEnumName(e, i));
Result := S;
end;
如果这是一个通用函数(所有 Enum):
如果 Enum 是 Continuity,如下代码是 OK(by rtti)
如果不是 Continuity 我现在找不到方法
type
TMyEnum = (s, b, c, d, e, f);
implementation
uses
System.Rtti, TypInfo;
procedure ParseEnum<T>(sl: TStrings);
var
rt: TRttiType;
rot: TRttiOrdinalType;
i: Integer;
begin
rt := TRttiContext.Create.GetType(TypeInfo(T));
rot := rt.AsOrdinal;
for i := rot.MinValue to rot.MaxValue do
sl.Add(GetEnumName(TypeInfo(T), i));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
ParseEnum<TMyEnum>(sl);
ShowMessage(sl.Text);
finally
sl.Free;
end;
end;
为什么sl作为参数但是结果:注意人们不要忘记释放