tags:

views:

139

answers:

3

In Java, one can save an object (of any type) to a file and load an object from a file. Can we do this in Delphi?

+2  A: 

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
splash
Published visibility is only needed when still using the "old style" RTTI. As of D2010 the extended RTTI does not require properties to have published visibility in order to be able to enumerate them. Plus it can enumerate a lot more than "just" properties.
Marjan Venema
+1  A: 

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

http://stackoverflow.com/questions/2657502/practical-usage-for-delphis-new-rtti-attributes-values/2657604#2657604 and http://stackoverflow.com/questions/2217068/why-should-i-care-about-rtti-in-delphi/2217243#2217243

Marjan Venema