tags:

views:

706

answers:

3
+1  Q: 

RTTI in Delphi

I'm trying to parse objects to XML in Delphy, so I read about calling the objet's ClassInfo method to get its RTTI info.

The thing is, this apparently only works for TPersistent objects. Otherwise, I have to specifically add a compiler directive {$M+} to the source code for the compiler to generate RTTI info.

So I happily added the directive, only to find that, even if it did return something from the ClassInfo call (it used to return nil), now I cannot retrieve the class' properties, fields or methods from it. It's like it created the object empty.

Any idea what am I missing here? Thanks!

+2  A: 

RTTI will only show you published properties,etc. - not just public ones.

Try your code with a TObject and see what happens - if that isn't working, post your code because not everyone is psychic.

Roddy
That was the problem, my properties were just public, not published.Will try when I'm back at work on monday. Thanks!
Pablo
+3  A: 

Did you put those properties and methods into the published section?

Besides that, 'classical' RTTI ($TYPEINFO ON) will only get you information on properties, not on methods. You need 'extended' RTTI ($METHODINFO ON) for those.

Good starting point for extended RTTI: David Glassborow on extended RTTI

(who would believe that just this minute I finished writing some code that uses extended RTTI and decided to browse the Stack Overflow a little:))

gabr
Thanks, that has to be the problem. I was putting the properties on the public, not the published section.I'll have to wait for monday to try, since I do Delphy at work, and I'm home for the weekend already.
Pablo
+2  A: 

Have you considered using the TXMLDocument component? It will look at your XML and then create a nice unit of Delphi classes that represents your XML file -- makes it really, really easy to read and write XML files.

Nick Hodges
TXMLDocument does that? I thought it was the XML Data Binding wizard that did that... ;)
Oliver Giesen