开发者

How can I check an interface property's visibility?

开发者 https://www.devze.com 2023-01-21 21:12 出处:网络
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 ca

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.


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


You can do it with RTTI easily enough. You can use the classic RTTI function GetPropInfo in the TypInfo unit. If it returns nil, then no published property by that name exists. Or you can look it up with extended RTTI and check the Visibility property, which will tell you what visibility level it's declared at.

0

精彩评论

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

关注公众号