|
|
|||||
|
|
|||||
|
|||||
|
|
Writing to Read Only Properties in Delphi Classes I guess this title could be construed as a little misleading as the code presented here really only works on a subset of read only properties, as specified below. So why write to read only properties, after all aren't properties defined as read only for a reason? Strictly speaking yes, but unfortunately in my experience there are times when the VCL (or for that matter any other third party code) simply doesn't offer as much flexibility as perhaps it should, often in the effort to make code as "idiot proof" as possible. Other times these properties aren't necessarily read only, but the methods that get called to update said properties might in turn call other methods you don't want called or won't update the property due to some unknown or uncontrollable constraint. Occasionally there are just times where the rules need to be bent. What properties can I write to with this code? The answer is simple - any published property whose "read specifier" physically represents a field and not a method. For example: type In this class definition, write access to property "MyVariable" would not ordinarily be allowed outside of the unit this class has been defined in. Moreover, if the property you are writing to contains a method type write specifier: property MyVariable: string read FMyVariable write SetMyVariable; then using VclPropMorph to update "MyVariable" means that the field "FMyVariable" is updated without method "SetMyVariable" ever having been called. What properties can't I update? Any non published property, or any published property whose read specifier represents a method and not a field of that class. For example if "MyVariable" was accessed (or read) through a method instead of referencing the physical field itself, then you would no longer be able to write to this property from outside of the unit, for example: type Whilst this might seem limited, it's actually relatively handy for modifying some of the published property definitions of the Delphi VCL (for example, the "Showing" property of TWinControl) to enable property values to be changed without having that property's write specifier method called. Requirements For this code to work you must compile your application with run time type information (RTTI) turned on. This can be done with the $M or $TYPEINFO directives in your code. Download Code VclPropMorph.pas can be downloaded here.
|
||||
|
Hello World. This is just some filler
space, nothing more. | |||||