rtti设置record的值

rtti设置record的值

uese system.rtti;

class procedure Trows.scan<T>(const aRec: T; instance: Pointer; ds: TDataSet);
begin
  var rtx: TRttiContext := TRttiContext.Create;
  var rt: TRttiType := rtx.GetType(TypeInfo(T));
  if (rt = nil) then Exit;
  for var rf: TRttiField in rt.AsRecord.GetFields do
    rf.SetValue(instance, ds.FieldByName(rf.Name).AsString);
  rtx.Free;
end;

 调用演示 

      var i: Integer := 0;
      db.qry.First;
      while not db.qry.Eof do
      begin
        Trows.scan<Tdanwei>(data.rows[i], @data.rows[i], db.qry);
        db.qry.Next;
        Inc(i);
      end;

  

posted @ 2023-08-21 11:56  delphi中间件  阅读(153)  评论(0编辑  收藏  举报