Thema: Delphi Object kopieren

Einzelnen Beitrag anzeigen

Poelser

Registriert seit: 21. Apr 2008
Ort: Europa
145 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: Onject kopieren

  Alt 28. Apr 2017, 11:50
Moin,

ich dupliziere mit Hilfe der Unit TypInfo:
Delphi-Quellcode:
class procedure TObjHelper.CopyObject(Source, Destination: TPersistent);
var
  TypInfo: PTypeInfo;
  PropList: TPropList;
  PropCount, i: integer;
  Value: variant;
begin
  assert(assigned(Source), 'Source ist NIL');
  assert(assigned(Destination), 'Destination ist NIL');
  TypInfo := Source.ClassInfo;
  PropCount := GetPropList(TypInfo, tkAny, @PropList);
  for i := 0 to PropCount - 1 do
  begin
    Value := GetPropValue(Source, string(PropList[i].Name));
    SetPropValue(Destination, string(PropList[i].Name), Value);
  end;
end;
Nicht zu vergessen: Das geht nicht mit TObject, du musst von TPersistent erben. Diese Prozedur erstellt auch kein Objekt, das muss vorher schon bestehen.

HTH, Poelser
  Mit Zitat antworten Zitat