0

为什么这不会编译?

(来自自定义 JSON 拦截器的代码)

procedure myproc(MyObject: TObject);

  procedure test(C: TClass);
  begin
    var ctx := TRttiContext.Create;
    var typ := ctx.GetType(C);
    for var fld in typ.GetFields do
    begin
      if fld.Name = 'anyfield' then
      begin
         TObjectList<Fld.ClassType>.Create(True);
      end;
    end;
    typ.Free;
    ctx.Free;
  end;

begin
  Test( MyOrgObject );
end;

出了点问题:TObjectList<Fld.ClassType>.Create(True);

有任何想法吗 ?谢谢

好的,我所拥有的是:

procedure TqBitObjectListInterceptor.StringReverter(Data: TObject; Field, Arg: string);
begin   
  if (Data is TqBitLogsType) and (Field = 'Flogs') then
  begin
    TqBitLogsType(Data).Flogs := TObjectList<TqBitLogType>.Create(True);
  end else
  if (Data is TqBitPrefsType) and (Field = 'Flist') then
  begin
    TqBitPrefsType(Data).Flogs := TObjectList<TqBitPrefsType>.Create(True);
  end else... 
end;
   

依此类推,对于具有相同代码的每一对类型/字段,我希望能够使用 RTTI 将 Data.Field 分配给 TObjectList :


procedure TqBitObjectListInterceptor.StringReverter(Data: TObject; Field, Arg: string);

  function Convert(C: TClass; F: string): boolean;
  if (Data is C) and (Field = F) then
  begin
    var ctx := TRttiContext.Create;
    var typ := ctx.GetType(Data);
    for var fld in typ.GetFields do
    begin
      if fld.Name = F then
      begin
        fld.SetValue( TObjectList<C>.Create(True) );
      end;
       TObjectList<C>.Create(True); <<<<<
      end;
    end;
    typ.Free;
    ctx.Free;
    Result := True;
  end;
  
begin
  Convert(TqBitLogsType, 'Flogs');
  Convert(TqBitPreferenceType, 'Flist');
end;

供参考: https ://sourceforge.net/p/qbitvcl/code/HEAD/tree/trunk/

单位:uqBitAPITypes.pas 过程 TqBitObjectListInterceptor.StringReverter(Data: TObject; Field, Arg: string);

4

0 回答 0