Saving a TObject to a File

前端 未结 7 1487
无人及你
无人及你 2021-02-04 12:45

How can one save an Object, in its current state, to a file? So that it can immediately be read and restored with all its variables.

7条回答
  •  后悔当初
    2021-02-04 13:44

    As already stated, the easiest way is to use a Stream and its WriteComponent and ReadComponent methods.
    But be aware that :
    - it works for descendants of TComponent, not plain TObject;
    - only for the published properties (those saved in a dfm), not the public ones nor (a fortiori) the privwte ones;
    - you have to pay a special attention for the Name property when restoring the component.

    You may find some code you could use in these SO answers: Replace visual component at runtime in Delphi, Duplicating components at Run-Time

提交回复
热议问题