枚举类型互相转换(使用GetEnumName和TypeInfo两个函数)

时间:2023-03-09 18:10:35
枚举类型互相转换(使用GetEnumName和TypeInfo两个函数)

uses
Classes,TypInfo ;

type
TCommandType = (ctEmptyCommand,ctAdd,ctModify);

TCommandTypeConvert=class
public
    class function CommandToString(ACommand: TCommandType): string;
    class function StringToCommand(const AStrCommand: string): TCommandType;
end;

implementation

class function TCommandTypeConvert.CommandToString(ACommand: TCommandType): string;
begin
Result := GetEnumName(TypeInfo(TCommandType),Ord(ACommand));
end;

class function TCommandTypeConvert.StringToCommand(const AStrCommand: string): TCommandType;
begin
Result := TCommandType(GetEnumValue(TypeInfo(TCommandType), AStrCommand));
end;

end.

http://blog.csdn.net/tercel99/article/details/7791652