uses SysUtils, TypInfo; type TSomeEnumType = (TheFirst, TheSecond, TheThird, TheFourth); type TEnumGen<TEnumType> = class class function Name(const Enum:TEnumType):String; class function Value(const Ordinal:Integer):TEnumType; end; class function TEnumGen<TEnumType>.Name(const Enum: TEnumType): String; begin Result := Format('%s.%s', [GetTypeName(TypeInfo(TEnumType)), GetEnumName(TypeInfo(TEnumType), Ord(Enum)) // <-- Bombs ]); end; class function TEnumGen<TEnumType>.Value(const Ordinal:Integer):TEnumType; begin Result := TEnumType(Ordinal); // <- Bombs end;
Unfortunately there is no "enum" delimiter that can be used to tell the compiler that Ord() and casting of integers should be allowed for generic enumerated type arguments.