2012年7月7日星期六

【转】delphi 枚举类型与字符串的转换

摘自:http://hi.baidu.com/%D4%F8%BD%F0%C1%BA/blog/item/38a6ed3a0495cee115cecb0e.html

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.

没有评论: