1
votes

I would like to use attributes to read/write in property values to/from the registry.

All of the examples I have looked use a Load/Save function to loop over all the properties and examine the attributes. Instead of having a Load/Save routine I would like to read the value from the registry when the property is read or written. However, I can't figure out how to find out the name of the current property in the Read method.

I know I could have a one line getter/setter for my properties that pass in the correct string values to the Read/Write methods. I was hoping I could use attributes. Then when I define simple classes with the properties I want to save and restore. I would not need to write have any code for those classes. Everything would be dealt with the in the base class.

It could be that this is not possible.

I'm taking this example from Robert Love as my starting point: http://robstechcorner.blogspot.de/2009/10/ini-persistence-rtti-way.html

type

  RegValueAttribute = class(TCustomAttribute)

   private
     FName: string;
     FDefaultValue: string;

   published
      constructor Create(const aName : string;const aDefaultValue : String = '');
     property Name : string read FName write FName;
     property DefaultValue : string read FDefaultValue write FDefaultValue;
   end; 


   TRegBaseClass = class
   protected
     procedure WriteString(AValue: string);
     function ReadString: string;
   end;

   TMyRegClass = class(TRegBaseClass)
   public
     [RegValueAttribute('MySavedProperty', 'DefaultValue')]
     property MySavedProperty: string read ReadString write WriteString;
   end;

  ///////////////////////////////////////////  

   function TRegBaseClass.ReadString: string;
   begin
     // ??  Is there any way to get the attributes for the property
     // that got me here.  
   end;

   procedure TRegBaseClass.ReadString(AValue: string);
   begin
     // ??  Is there any way to get the attributes for the property
     // that got me here.  
   end;
2

2 Answers

3
votes

A given getter/setter can be used for multiple properties. The only way for a getter/setter to know which property is the caller is to use the index specifier on the property declaration, which then gets passed to the getter/setter as an input parameter. The getter/setter can then use RTTI to loop through the owning object's properties looking for the property with the specified index, and then use the attributes of that property as needed.

0
votes

The relationship is that the the property has associated with it a getter and a setter. The getter/setter itself has no a priori knowledge that it is being used as a getter/setter. For instance, multiple properties could use the same getter/setter.

So the best you can do, as it stands, is to iterate over all the properties of the type, using RTTI, looking for the currently executing method as a property getter/setter. That does not sound like much fun.

I suspect that the best attribute based approach involves attaching attributes to the getter/setter methods.