gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:47:40 25 4
gpt4 key购买 nike

我创建了自己的组件,并且有这个属性:

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 流系统有一天发生更改和困惑,此类代码将继续工作订单。

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

关于delphi - 如何将我的自定义属性设置为第一个从流系统读取的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com