【发布时间】:2014-01-04 18:02:59
【问题描述】:
我想使用属性在注册表中读取/写入属性值。
我看过的所有示例都使用加载/保存函数来循环所有属性并检查属性。我不想在读取或写入属性时从注册表中读取值,而不是加载/保存例程。但是,我不知道如何在 Read 方法中找出 当前属性 的名称。
我知道我可以为我的属性设置一行 getter/setter,将正确的字符串值传递给 Read/Write 方法。我希望我可以使用属性。然后,当我使用要保存和恢复的属性定义简单类时。我不需要为这些类编写任何代码。一切都将在基类中处理。
这可能是不可能的。
我以 Robert Love 的这个例子为出发点: 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;
【问题讨论】:
标签: delphi delphi-xe3