如何使用RTTI修改delphi属性Getter/Setter? [英] How to modify delphi property Getter/Setter with RTTI?

查看:60
本文介绍了如何使用RTTI修改delphi属性Getter/Setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RTTI替换属性的getter/setter.

I would like to replace the getter/setter for properties using RTTI.

我知道您可以使用TPropInfo.SetProc/GetProc访问getter设置程序,并且我知道这些字段指向不同的数据,具体取决于该属性是使用虚拟方法,直接字段访问还是静态方法.

I know that you can access the getter setter with TPropInfo.SetProc/GetProc and I know that these fields points to different data depending if the property uses virtual methods, direct field access or static methods.

我很感兴趣用自定义虚拟方法替换指向虚拟方法的属性设置器/获取器.

I'm interesting on replacing propertiy setters/getters that point to virtual methods with custom virtual methods.

TRttiInstanceProperty(RttiProperty).PropInfo^.SetProc := ? // SomeOtherInstance.Setter
TRttiInstanceProperty(RttiProperty).PropInfo^.GetProc := ? // SomeOtherInstance.Getter

推荐答案

您不能以这种方式实现目标,因为您的问题是基于错误的观念.RTTI信息为您提供了已编译代码中指定的吸气剂/设置剂.但是,当您访问属性时,不会查询RTTI信息.而是直接调用getter/setter.

You cannot achieve your goal this way because your question is based on a mis-conception. The RTTI information gives you the getter/setter as specified in the compiled code. But when you access a property, the RTTI information is not consulted. Rather the getter/setter is called directly.

为说明起见,请考虑以下规范的只读属性:

To illustrate, consider the following canonical read only property:

property Count: Integer read GetCount;

您可以使用RTTI查询此属性,以找出实现getter的方法.但是,当您使用代码编写此代码时:

You can query this property with RTTI to find out the method that implements the getter. However when you write this in code:

Writeln(Obj.Count);

编译器将此翻译为:

Writeln(Obj.GetCount);

并进行编译.在呼叫站点上,永远不会查询RTTI信息.因此,任何修改RTTI信息的尝试都不会影响访问该属性的代码.

and compiles that. At the call site the RTTI information is never consulted. So any attempt to modify the RTTI information will have no impact on code that accesses the property.

您需要找到解决问题的其他方法.

You need to find a different solution to your problem.

这篇关于如何使用RTTI修改delphi属性Getter/Setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆