我想使用属性在注册表中读取/写入属性值。
我看过的所有示例都使用加载/保存函数来循环所有属性并检查属性。我不想在读取或写入属性时从注册表中读取值,而不是加载/保存例程。但是,我不知道如何在 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;
最佳答案
给定的 getter/setter 可用于多个属性。 getter/setter 知道调用者是哪个属性的唯一方法是使用 index
属性声明上的说明符,然后将其作为输入参数传递给 getter/setter。然后,getter/setter 可以使用 RTTI 循环遍历拥有对象的属性,以查找具有指定 index
的属性。 ,然后根据需要使用该属性的属性。
关于delphi - 我可以在读写方法中访问属性的属性吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20924631/