如何将我的自定义属性设置为第一个从流系统中读取的属性?



我创建了自己的组件,并且具有以下属性:

property DefStyleAttr: String read fDefStyleAttr write SetDefStyleAttr;

我希望此属性是在组件创建期间第一个加载的属性。您知道,当流系统从其窗体文件加载窗体或数据模块时,它首先通过调用其构造函数来构造窗体组件,然后从窗体文件中读取其属性值。在这里,我希望第一个阅读DefStyleAttr。

组件属性按定义顺序加载(保存)。祖先属性首先按继承顺序加载。

如果您有类的层次结构,例如:

TBase = class(TComponent)
protected
FFirst: integer;
FSecond: integer;
published
property First: integer read FFirst write FFirst;
property Second: integer read FSecond write FSecond;
end;
TFirstDescendant = class(TBase)
protected
FThird: integer;
published
property Third: integer read FThird write FThird;
end;
TSecondDescendant = class(TFirstDescendant)
protected
FFourth: integer;
published
property Fourth: integer read FFourth write FFourth;
end;

然后创建并流式传输类型TSecondDescendant的实例,它将如下所示:

object TSecondDescendant
First = 1
Second = 2
Third = 3
Fourth = 4
end

因此,如果你在类层次结构中需要DefStyleAttr成为第一个,你必须在所有其他属性之前声明它已发布。

例如,如果您的层次结构以TBase开头,则必须在First之前添加它

TBase = class(TComponent)
...
published
property DefStyleAttr: String read fDefStyleAttr write SetDefStyleAttr;
property First: integer read FFirst write FFirst;
property Second: integer read FSecond write FSecond;
end;
object TSecondDescendant
DefStyleAttr = 'abc'
First = 1
Second = 2
Third = 3
Fourth = 4
end

但是,如果层次结构以TFirstDescendant开头,并且必须向该类添加DefStyleAttr,则只会在类的属性之后流式传输TBase该类。

TFirstDescendant = class(TBase)
...
published
property DefStyleAttr: String read fDefStyleAttr write SetDefStyleAttr;
property Third: integer read FThird write FThird;
end;
object TSecondDescendant
First = 1
Second = 2
DefStyleAttr = 'abc'
Third = 3
Fourth = 4
end

解决处理依赖于流式处理属性顺序的代码时的一些问题。

此类代码的主要问题是,没有内置的(编译器)机制可以确保如果有人意外更改已发布属性的顺序,或者(极不可能)Delphi 流系统有一天更改并弄乱顺序,此类代码将继续工作。

如果代码被记录下来 - 具有此类属性的地方被清楚地标记,并且如果存在单元测试,如果顺序更改会中断,那么这样的代码可以安全使用,并且不会比任何人都可以编写的任何其他代码或多或少地存在问题或脆弱。

最新更新