将TObject保存到文件 [英] Saving a TObject to a File
问题描述
如何在目前的状态下将对象保存到文件?所以它可以立即被读取和恢复所有的变量。
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.
推荐答案
如上所述,最简单的方法是使用流及其 WriteComponent 和 ReadComponent 方法。
但请注意:
- 它对TComponent 的后代有效,而不是纯TObject;
- 只有已发布的属性(保存在dfm中的那些),而不是公开的,而不是更贵的;
- 恢复组件时,您必须特别注意Name属性。
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.
您可能会在这些SO答案中找到一些代码:在Delphi中运行时替换可视化组件,运行时复制组件
You may find some code you could use in these SO answers: Replace visual component at runtime in Delphi, Duplicating components at Run-Time
这篇关于将TObject保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!