gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:23:21 28 4
gpt4 key购买 nike

我需要对泛型类型进行“智能”检测并以字符串形式返回,但目前我不明白为什么 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 中,输出为:

:3ByteByte

In XE6 and later the output is:

ShortIntByteByte

It looks as though the earlier versions of Delphi create a private type with an unspeakable name when inferring from a literal of 0. I cannot say why that should be so. Since the behaviour has changed, one can only assume that the Embarcadero engineers made the change to fix what they deemed to be a defect.

In other words, it would seem that the behaviour that you are observing is a bug.

My hypothesis that a private type is created is backed up by this program:

{$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 中修复的错误。

我对如何解决此问题没有任何建议,因为我不知道您的实际问题。也就是说,如果可能的话,通常最好避免比较类型名称。

关于delphi - 通用整数的奇怪 PTypeInfo 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674887/

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