Delphi: RTTI for indexed properties in 2010?

后端 未结 1 1405
清歌不尽
清歌不尽 2021-01-21 04:56

Please forgive the verbosity of the following code example. Using Delphi 2009, I created the two classes TOtherClass and TMyClass:

TOtherClass = class(TObject)
p         


        
相关标签:
1条回答
  • 2021-01-21 05:31

    Yes, if all the property reader does is index into an array field or list-class field, then you can use RTTI to index into the field directly. This is kind of fragile, though, since it breaks your encapsulation, requiring you to write code to a specific implementation detail instead of a general principle, which is what RTTI is mainly good for. Your RTTI code has to match the exact structure of your class, and if it changes you have to change the code as well. That sort of defeats the purpose of using RTTI.

    But, if there's no alternative available, since array properties have no RTTI for them, it may be the only way, for now at least.

    EDIT: Updating this answer. Support for indexed properties was added to the extended RTTI system in XE2. (However, due to unrelated stability issues, you might want to wait for XE3...)

    0 讨论(0)
提交回复
热议问题