gpt4 book ai didi

delphi - 如何使用通用枚举类型调用 Get Enum Name?

转载 作者:行者123 更新时间:2023-12-03 14:52:39 32 4
gpt4 key购买 nike

我有一个使用枚举泛型类型的泛型类。我的问题如何在该类型的实例上使用 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。那么这可以做到吗?如果是的话怎么办?

最佳答案

就我个人而言,我会通过调用 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方法可以满足您的需要,并且我相信您将能够使其适应您的类(class)。

如果您不喜欢使用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 .

关于delphi - 如何使用通用枚举类型调用 Get Enum Name?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27901990/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com