Fra*_*anz 1 delphi enums tstringlist typeinfo
我的函数EnumToStringList()本身可以编译,但我无法使用应用程序中的任何枚举类型调用它:
[dcc64 错误] Unit_test.pas(119): E2029 '(' 预期但 ')' 找到
我的方法有什么问题吗?
function EnumToStringList(const TypeInfo: pTypeInfo): TStringlist;
var i : Integer;
begin
Result := TStringList.Create;
for i := GetTypeData(TypeInfo)^.MinValue to GetTypeData(TypeInfo)^.MaxValue do
begin
result.add (GetEnumName(TypeInfo, i));
end;
end;
function EnumToString(const TypeInfo: pTypeInfo; Ix: Integer): string;
begin
result := GetEnumName(TypeInfo, Ix);
end;
type
TProjectTypes = (low,hot,skip,pause,others);
// test code:
MyProjectStrings := EnumToStringList (TProjectTypes);
Run Code Online (Sandbox Code Playgroud)
您不能直接将枚举类型传递给您的函数。您需要首先通过调用来获取其类型信息TypeInfo。
var
MyProjectStrings := EnumToStringList(TypeInfo(TProjectTypes));
Run Code Online (Sandbox Code Playgroud)