将 TObject 保存到文件 [英] Saving a TObject to a File

查看:35
本文介绍了将 TObject 保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将当前状态下的对象保存到文件中?这样就可以立即读取并恢复它的所有变量.

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.

推荐答案

如前所述,最简单的方法是使用 Stream 及其 WriteComponentReadComponent 方法.
请注意:
- 它适用于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆