我有一个使用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;
我正在使用Delphi XE。那可以这样做吗?如果是这样的话?
答案 0 :(得分:5)
就个人而言,我会打电话给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;
ToOrdinal
方法可以满足您的需求,并确保您能够将其与您的班级相匹配。
如果您不想以这种方式使用Move
,那么您可以使用TValue
。
TValue.From<TKey>(Key).AsOrdinal
@TLama指出,您可以使用
来避免调用GetEnumName
TValue.From<TKey>(Key).ToString
从表面上看,使用TValue
似乎更符合仿制药和RTTI的精神。对Move
的调用依赖于枚举类型的特定实现细节。但是,通过调试器并观察执行TValue.From<TKey>(Key).AsOrdinal
时涉及多少代码非常有趣。仅此一点就足以让我犹豫推荐使用TValue
。
另一种实现此目的的方法是使用TRttiEnumerationType
:
TRttiEnumerationType.GetName<TKey>(Key)
实施此功能比使用TValue.ToString
更有效,只不过是对GetEnumName
的调用。
答案 1 :(得分:1)
这是我班级的更新版本,建议进行更改。感谢David和TLama
uses
TypInfo, Rtti;
type
TEnumSettings<TKey: record> = class
private
Key: TKey;
public
constructor Create(aKey: TKey);
function ToString: string; override;
end;
{ TEnumSettings<TKey> }
constructor TEnumSettings<TKey>.Create(aKey: TKey);
begin
if PTypeInfo(System.TypeInfo(TKey)).Kind <> tkEnumeration then
raise Exception.Create(string(PTypeInfo(System.TypeInfo(TKey)).Name) + ' is not an Enumeration');
Key := aKey;
end;
function TEnumSettings<TKey>.ToString: string;
begin
Result := TValue.From<TKey>(Key).ToString;
end;
还有一个小测试例子:
将代码复制到From:
的OnCreate中procedure TForm1.FormCreate(Sender: TObject);
begin
with TEnumSettings<boolean> .Create(True) do
try
Caption := ToString;
finally
Free;
end;
end;