Search code examples
delphirtti

How can I check an interface property's visibility?


I have started writing web services in Delphi 2010 and am unit testing to make sure they function as planned. My unit tests of the code passed but one web service method didn’t return a value when called as a service (i.e. through SoapUI). After many hours of searching through the code I discovered it was because the properties on my return object weren’t in the published section of the interface; they were in the public section.

Is there a way for my unit tests to check variable visibility on objects so I can avoid this problem in the future? I was trying to find a way with RTTI but haven’t been able to find anything.


Solution

  • You can determine whether a property was declared published by attempting to access that property's RTTI. A public property has no RTTI, a published property does.

    Something like this:

    if (GetPropInfo(myobject, "PropertyName") != null) then 
        // it's published...
    

    For more info on RTTI, see Brian Long's article: http://www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm