我有一个使用Enum Generic Type的Generic类.我的问题如何在该类型的实例上使用GetEnumName?
我已经创建了一个小型演示类来说明问题:
type
TEnumSettings<TKey: record > = class
private
Key: TKey;
public
constructor Create(aKey: TKey);
function ToString: string; override;
end;
uses
TypInfo;
{ TEnumSettings<TKey> }
constructor TEnumSettings<TKey>.Create(aKey: TKey);
begin
if PTypeInfo(System.TypeInfo(TKey)).Kind <> tkEnumeration then
Exception.Create(string(PTypeInfo(System.TypeInfo(TKey)).Name) + ' is not an Enumeration');
Key := aKey;
end;
function TEnumSettings<TKey>.ToString: string;
begin
Result := GetEnumName(System.TypeInfo(TKey), Integer(Key)) <== HERE I get a compile error: Invalid type cast
end;
Run Code Online (Sandbox Code Playgroud)
我正在使用Delphi XE.那可以这样做吗?如果是这样怎么样?
就个人而言,我会打电话给Move
.我有以下类型:
type
TEnumeration<T: record> = class
strict private
class function TypeInfo: PTypeInfo; inline; static;
class function TypeData: PTypeData; inline; static;
public
class function IsEnumeration: Boolean; static;
class function ToOrdinal(Enum: T): Integer; inline; static;
class function FromOrdinal(Value: Integer): T; inline; static;
class function MinValue: Integer; inline; static;
class function MaxValue: Integer; inline; static;
class function InRange(Value: Integer): Boolean; inline; static;
class function EnsureRange(Value: Integer): Integer; inline; static;
end;
{ TEnumeration<T> }
class function TEnumeration<T>.TypeInfo: PTypeInfo;
begin
Result := System.TypeInfo(T);
end;
class function TEnumeration<T>.TypeData: PTypeData;
begin
Result := TypInfo.GetTypeData(TypeInfo);
end;
class function TEnumeration<T>.IsEnumeration: Boolean;
begin
Result := TypeInfo.Kind=tkEnumeration;
end;
class function TEnumeration<T>.ToOrdinal(Enum: T): Integer;
begin
Assert(IsEnumeration);
Assert(SizeOf(Enum)<=SizeOf(Result));
Result := 0; // needed when SizeOf(Enum) < SizeOf(Result)
Move(Enum, Result, SizeOf(Enum));
Assert(InRange(Result));
end;
class function TEnumeration<T>.FromOrdinal(Value: Integer): T;
begin
Assert(IsEnumeration);
Assert(InRange(Value));
Assert(SizeOf(Result)<=SizeOf(Value));
Move(Value, Result, SizeOf(Result));
end;
class function TEnumeration<T>.MinValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MinValue;
end;
class function TEnumeration<T>.MaxValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MaxValue;
end;
class function TEnumeration<T>.InRange(Value: Integer): Boolean;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.InRange(Value, ptd.MinValue, ptd.MaxValue);
end;
class function TEnumeration<T>.EnsureRange(Value: Integer): Integer;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.EnsureRange(Value, ptd.MinValue, ptd.MaxValue);
end;
Run Code Online (Sandbox Code Playgroud)
该ToOrdinal
方法可以满足您的需求,我相信您将能够适应您的课程.
如果您不喜欢Move
以这种方式使用,那么您可以使用TValue
.
TValue.From<TKey>(Key).AsOrdinal
Run Code Online (Sandbox Code Playgroud)
并且@TLama指出你可以GetEnumName
通过使用来避免打电话
TValue.From<TKey>(Key).ToString
Run Code Online (Sandbox Code Playgroud)
从表面上看,使用TValue
似乎更符合仿制药和RTTI的精神.调用Move
依赖于枚举类型的特定实现细节.但是,通过调试器并观察执行中涉及多少代码是非常有趣的TValue.From<TKey>(Key).AsOrdinal
.仅此一点就足以让我对推荐使用犹豫不决TValue
.
另一种实现此目的的方法是使用TRttiEnumerationType
:
TRttiEnumerationType.GetName<TKey>(Key)
Run Code Online (Sandbox Code Playgroud)
这种方法的实现比使用它更有效,只不过TValue.ToString
是对它的调用GetEnumName
.