Question

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

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
Was it helpful?

Solution

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;

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);

the compiler translates this to:

Writeln(Obj.GetCount);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top