【问题标题】:How can I call GetEnumName with a generic enumerated type?如何使用通用枚举类型调用 GetEnumName?
【发布时间】:2021-02-26 13:27:30
【问题描述】:

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

【问题讨论】:

  • 我认为您不应该使用移动命令。预期的方式是使用TValue 相关代码,例如TValue.From&lt;TKey&gt;(Key).AsInteger,我会说。
  • @TLama 这导致EInvalidCast
  • @TLama 不,你可以用TValue 做到这一点。使用AsOrdinal。我将其添加到答案中。
  • 其实简单的Result := TValue.From&lt;TKey&gt;(Key).ToString; 效果很好。
  • 嗨 Jens,您可以自己编一个答案,而不是在您的问题中编辑解决方案。

标签: delphi generics


【解决方案1】:

就我个人而言,我会打电话给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&lt;TKey&gt;(Key).AsOrdinal 所涉及的代码量是非常有趣的。仅此一项就足以让我犹豫是否推荐使用TValue

实现此目的的另一种方法是使用TRttiEnumerationType

TRttiEnumerationType.GetName<TKey>(Key)

这个实现比使用TValue.ToString 更有效,只不过是调用GetEnumName

【讨论】:

  • 您的带有 move 的版本与大端系统不兼容
  • Delphi 总是小端序
  • 好的,我认为移动设备是BE。显然他们都可以。
  • ARM 是双端模式,Emba 以小端模式运行。
【解决方案2】:

这是我班级的更新版本,建议进行更改。感谢 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;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多