如何将 TClass 转换为 T?

How to convert TClass to T?

我使用 RTTI (SuperObject) 将 JSON 转换为对象:

class function RecordJson.Json2Record<T>(const obj: ISuperObject): T;
var
  ctx: TSuperRttiContext;
begin
  ctx := TSuperRttiContext.Create;
  try
    Result := ctx.AsType<T>(obj);
  finally
    ctx.Free;
  end;
end;

我用的是这种方式,效果很好:

if aSo.o['TDistanceBhTopConv'] <> nil then
  Result := RecordJson.Json2Record<TDistanceBhTopConv>(aSo.o['TDistanceBhTopConv']);

但我有很多 classes。所以我创建了一个TDictionary来记录字符串和class的关系,现在我想使用下面的代码:

FClassDic: TDictionary<string, TClass>;
FClassDic.Add('TDistanceValTopConv', TDistanceValTopConv);
FClassDic.Add('TDistanceBhTopConv', TDistanceBhTopConv);
FClassDic.Add('TLbXsConv', TLbXsConv);
FClassDic.Add('TConcreteConv', TConcreteConv);
for Key in FClassDic.Keys do
  if aSo.o[Key] <> nil then
  begin
    Result := RecordJson.Json2Record<FClassDic.Items[Key]>(aSo.o[Key]);
  end;

但是无法编译:

E2250 There is no overloaded version of 'Json2Record' that can be called with these arguments

我知道这是因为TClass(实例的class)和T(实例)的不同。

有什么办法可以解决吗?

您可以尝试使用对象创建过程创建字典,但如果真的值得的话...

type
  // Object construction function type
  TCreateObjectProc = reference to function(const ctx: TSuperRttiContext; obj: ISuperObject): TObject;

  // Dictionary specification
  FCreateDict: TDictionary<string, TCreateObjectProc>;

然后用对象构造函数填充字典

  // Dictionary initialisation
  FCreateDict.Add('TDistanceValTopConv',
    function(const ctx: TSuperRttiContext; obj: ISuperObject): TObject
    begin
      Result := ctx.AsType<TDistanceValTopConv>(obj);
    end);

  FCreateDict.Add('TDistanceBhTopConv',
    function(const ctx: TSuperRttiContext; obj: ISuperObject): TObject
    begin
      Result := ctx.AsType<TDistanceBhTopConv>(obj);
    end);

  FCreateDict.Add('TLbXsConv',
    function(const ctx: TSuperRttiContext; obj: ISuperObject): TObject
    begin
      Result := ctx.AsType<TLbXsConv>(obj);
    end);

  FCreateDict.Add('TConcreteConv',
    function(const ctx: TSuperRttiContext; obj: ISuperObject): TObject
    begin
      Result := ctx.AsType<TConcreteConv>(obj);
    end);

然后使用

  for Key in FCreateDict.Keys do
    if aSo.o[Key] <> nil then
    begin
      ctx := TSuperRttiContext.Create;
      try
        Result := FCreateDict.Items[Key](ctx, aSo.o[Key]);
      finally
        ctx.Free;
      end;
    end;

而当使用 Remy 的 TSerializer<T: class> class 时:

type
  TSerializer<T: class> = class
  public
    class function Deserialize(const ctx: TSuperRttiContext; obj: ISuperObject): TObject;
  end;

class function TSerializer<T>.Deserialize(const ctx: TSuperRttiContext;
  obj: ISuperObject): TObject;
begin
  Result := ctx.AsType<T>(obj);
end;

然后有关字典初始化的代码如下所示(使用 Delphi 10.3 编译)

  FCreateDict.Add('TDistanceValTopConv', TSerializer<TDistanceValTopConv>.Deserialize);
  FCreateDict.Add('TDistanceBhTopConv', TSerializer<TDistanceBhTopConv>.Deserialize);
  FCreateDict.Add('TLbXsConv', TSerializer<TLbXsConv>.Deserialize);
  FCreateDict.Add('TConcreteConv', TSerializer<TConcreteConv>.Deserialize);