2

我正在尝试在 Delphi Rio 中编译以下代码:

unit untObjectHelper;

interface

uses
   SysUtils;

type
   TObjectHelper = class(TInterfacedObject)
   public
      class procedure Clone(const objOrigem: TObject; const objDestino: TObject);
   end;

implementation

uses
   System.Rtti;

{ TObjectHelper }

class procedure TObjectHelper.Clone(const objOrigem,
   objDestino: TObject);
begin
   if not Assigned(objOrigem) then
      Exit;

   if not Assigned(objDestino) then
      Exit;

   if objOrigem.ClassType <> objDestino.ClassType then
      Exit;

   var contexto := TRttiContext.Create;
   try
      var tipo := contexto.GetType(objOrigem.ClassType);
      var campos := tipo.GetFields();
   finally
      contexto.Free;
   end;
end;

end.

但是会发生以下错误:

[dcc32 致命错误] untObjectHelper.pas (36): F2084 内部错误: NC1921

在线上:

var fields: = type.GetFields ();

版本:Embarcadero® Delphi 10.3 版本 26.0.33219.4899

我没有找到有关此错误的参考,有人可以帮助我吗?非常感谢您

4

1 回答 1

2

问题在于类型推断,感谢 Rudy Velthuis 的提示

unit untObjectHelper;

interface

uses
   SysUtils;

type
   TObjectHelper = class(TInterfacedObject)
   public
      class procedure Clone(const objOrigem: TObject; const objDestino: TObject);
   end;

implementation

uses
   System.Rtti;

{ TObjectHelper }

class procedure TObjectHelper.Clone(const objOrigem,
   objDestino: TObject);
begin
   if not Assigned(objOrigem) then
      Exit;

   if not Assigned(objDestino) then
      Exit;

   if objOrigem.ClassType <> objDestino.ClassType then
      Exit;

   var contexto := TRttiContext.Create;
   try
      var tipo := contexto.GetType(objOrigem.ClassType);
      var campos: TArray<TRttiField> := tipo.GetFields();
   finally
      contexto.Free;
   end;
end;

end.
于 2019-05-06T13:30:02.327 回答