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

delphi – 通用整数的奇怪PTypeInfo名称

发布时间:2020-12-15 09:21:15 所属栏目:大数据 来源:网络整理
导读:我需要对泛型类型进行“智能”检测并返回为字符串,但此刻我不明白为什么delphi会在PTypeInfo.Name属性上添加一些奇怪的标识. 到目前为止我所拥有的是: program SO_29674887;{$APPTYPE CONSOLE}{$R *.res}uses System.SysUtils,System.TypInfo;type TypeReso
我需要对泛型类型进行“智能”检测并返回为字符串,但此刻我不明白为什么delphi会在PTypeInfo.Name属性上添加一些奇怪的标识.

到目前为止我所拥有的是:

program SO_29674887;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,System.TypInfo;

type
  TypeResolver = class
  strict private
    class function ReCast<T>(const AValue) : T;
  public
    class function Format<T>(const AValue : T) : string;
  end;

  Compare = class
  public
    class function IsEqual<A;B>(const AVal : A; BVal : B) : string;
  end;

{ TypeResolver }

class function TypeResolver.ReCast<T>(const AValue): T;
begin
  Result := T(AValue);
end;

class function TypeResolver.Format<T>(const AValue: T): string;
var Info : PTypeInfo;
begin
  Info := TypeInfo(T);

  Result := 'undefined';

  if(Info.Kind = tkInteger) then
    begin
      if(Info.Name = GetTypeName(TypeInfo(Byte))) then
        Result := IntToStr(ReCast<Byte>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(ShortInt))) then
        Result := IntToStr(ReCast<ShortInt>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(SmallInt))) then
        Result := IntToStr(ReCast<SmallInt>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(Integer))) then
        Result := IntToStr(ReCast<Integer>(AValue));
    end;

  Result := Info.Name + ':' + Result;
end;

{ Compare }

class function Compare.IsEqual<A,B>(const AVal: A; BVal: B): string;
begin
  Result := Format('%s = %s',[
    TypeResolver.Format<A>(AVal),TypeResolver.Format<B>(BVal)]);
end;

var PAUSE : string;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    WriteLn(Compare.IsEqual(0,255));
    WriteLn(Compare.IsEqual(-127,127));
    WriteLn(Compare.IsEqual(0,65535));
    WriteLn(Compare.IsEqual(-32768,32767));
    WriteLn(Compare.IsEqual(0,4294967295));
    WriteLn(Compare.IsEqual(-2147483648,2147483647));
    Readln(PAUSE);
  except
    on E: Exception do
      Writeln(E.ClassName,': ',E.Message);
  end;
end.

这里发生的是,而不是给我整数的实际类型名称,delphi给了我奇怪的标识符,如:2,:4,:6等.

例如:

Compare.IsEqual(0,255); //Gives Info.Name = :2,Byte
Compare.IsEqual(-127,127); //Gives Info.Name = ShortInt,:4
Compare.IsEqual(0,65535); //Gives Info.Name = :6,Word
Compare.IsEqual(-32768,32767); //Gives Info.Name = SmallInt,:8
Compare.IsEqual(0,4294967295); //Gives Info.Name = :01,Cardinal
Compare.IsEqual(-2147483648,2147483647); //Gives Info.Name = Integer,:21

所以我的问题是,如果在我看来delphi在提供这些标识符时没有提供实际类型的任何线索,以及为什么它给出了那些奇怪的标识符,我怎么能找到正确的类型.

解决方法

这是我的复制品,有点短.

{$APPTYPE CONSOLE}

uses
  System.TypInfo;

type
  TypeResolver = class
  public
    class function Format<T>(const AValue : T) : string;
  end;

class function TypeResolver.Format<T>(const AValue: T): string;
var
  Info: PTypeInfo;
begin
  Info := TypeInfo(T);
  Result := Info.Name;
end;

begin
  Writeln(TypeResolver.Format(0));
  Writeln(TypeResolver.Format<Byte>(0));
  Writeln(TypeResolver.Format(255));
  Readln;
end.

在XE2中,输出为:

:3
Byte
Byte

在XE6及更高版本中,输出为:

ShortInt
Byte
Byte

看起来Delphi的早期版本在从字面值0推断时创建了一个具有难以形容的名称的私有类型.我不能说为什么会这样.由于行为已经改变,人们只能假设Embarcadero工程师做出改变以修复他们认为是缺陷的东西.

换句话说,您所观察到的行为似乎是一个错误.

我创建私有类型的假设由此程序支持:

{$APPTYPE CONSOLE}

uses
  System.SysUtils,System.TypInfo;

type
  TypeResolver = class
  public
    class function Format<T>(const AValue : T) : string;
  end;

class function TypeResolver.Format<T>(const AValue: T): string;
var
  Info: PTypeInfo;
  TypeData: PTypeData;
begin
  Info := TypeInfo(T);
  Result := Info.Name;
  if Info.Kind=tkInteger then begin
    TypeData := GetTypeData(Info);
    Result := Result + ',min = ' + IntToStr(TypeData.MinValue) + 
      ',max = ' + IntToStr(TypeData.MaxValue);
  end;
end;

begin
  Writeln(TypeResolver.Format(0));
  Readln;
end.

在XE2上输出是:

:3,min = 0,max = 127

在XE6上,输出是:

ShortInt,min = -128,max = 127

所以我认为这是泛型类型推断的问题,我们可能归因于XE6中修复的错误.

我对你应该如何解决这个问题没有任何建议,因为我不知道你的实际问题.也就是说,比较类型名称通常是最好的,如果可能的话.

(编辑:李大同)

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

    推荐文章
      热点阅读