开发者

How to save an object to a file in Delphi

In Java, one can save a serializable object to a file and lo开发者_如何学JAVAad an object from that file. Can we do this in Delphi? If yes, How?


If you ask "Can we do this in Delphi?" - the answer is: yes. I guess you want to know, HOW this can be done in Delphi? Well this depends on what do you want to serialize:

If you want to serialize

  • your own data objects, then you could write your own saveTo/loadFrom functions
  • components, then you could use the built-in component streaming system
  • any unknown object, then you need some RTTI techniques and the objects must publish their serializable properties


http://blog.dragonsoft.us/2008/04/21/how-to-serialize-delphi-object/
DeHL: http://alex.ciobanu.org/?p=285


The first article linked to by Andre seems (maybe I skimmed to fast) to use the "old" pre-D2010 style RTTI (Run-Time Type Information), which is dependent on properties having published visibility in order to be able to enumerate them.

As of D2010 Delphi has a completely new extended RTTI (built on top of the old one) which does not require published visibility and does a lot more than enumerate properties. DeHL, as described in the second article linked to be Andre, does use the new RTTI, and if you are looking for a library, I would also recommend this library.

If you prefer to build your own, you could have a look at an article on XML-serialization using the new RTTI by Robert Love: http://robstechcorner.blogspot.com/2009/10/xml-serialization-basic-usage.html

Robert Love also has a nice overview of RTTI articles: http://robstechcorner.blogspot.com/2009/09/so-what-is-rtti-rtti-is-acronym-for-run.html

And for some more ideas on what can be done with the new style RTTI, have a look at

Practical usage for Delphi's new RTTI - Attributes,Values and Why should I care about RTTI in Delphi?


NativeXML NativeXML website has the ability to store/retrieve objects in an XML format, which gives you additional manipulation capability over the RTTI methodology. Fast and easy manipulation of XML documents and the added capability of saving/loading persistent objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜