加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何使用通用枚举类型调用GetEnumName?

发布时间:2020-12-15 04:35:05 所属栏目:大数据 来源:网络整理
导读:我有一个使用Enum Generic Type的Generic类.我的问题如何在该类型的实例上使用GetEnumName? 我已经创建了一个小型演示类来说明问题: type TEnumSettingsTKey: record = class private Key: TKey; public constructor Create(aKey: TKey); function ToStrin
我有一个使用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.那可以这样做吗?如果是这样怎么样?

解决方法

就个人而言,我会通过调用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,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.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的调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读