在Delphi中初始化单元之前添加代码 [英] Add code before initialization of units in Delphi

查看:84
本文介绍了在Delphi中初始化单元之前添加代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某个地方可以添加将在单元初始化之前执行的代码吗?

Is there a place where I can add code that will be executed before unit initialization?

我要这样做的原因是我需要更改DecimalSeparator,这必须在某些单元初始化之前完成。我已经将它放在Application.Initialize之前的项目源中,但是到那时已经太晚了。

The reason I want to do this is I need to change the DecimalSeparator, this has to be done before the initialization of some units. I have put it in the project source, before Application.Initialize but it is too late by then.

我看到的唯一选择就是将其放入需要更改DecimalSeparator的单元的初始化是这种情况吗?

As I see it the only choice I have is to put it in the initialization of the unit that needs the DecimalSeparator to be changed, is this the case?

在此先感谢您提供任何建议。

Thanks in advance for any advice.

推荐答案

Delphi中的初始化顺序是确定性的:单元的初始化顺序与编译器编译它们的顺序相同,并以相反的顺序完成。编译器从DPR的 uses 子句的顶部开始,一直向下移动,对于找到的每个单元,它都递归执行相同的操作:从 uses的开始子句,尝试编译每个尚未编译的使用过的单元,然后编译当前单元。因此,如果您可以在其他任何单元都未编译之前就将其插入,那么它将首先被初始化。

Initialization order in Delphi is deterministic: units get initialized in the same order as the compiler compiled them, and finalized in the reverse order. The compiler starts at the top of the DPR's uses clause and works its way down, and for each unit it finds, it does the same thing recursively: start at the start of the uses clause, try to compile each used unit that isn't already compiled, then compile the current unit. So if you can get your unit in before any of the other ones get compiled, then it will get initialized first.

如果要确保先执行它,制作一个新的单元,将您的更改放入该单元的初始化块中,然后确保它在依赖于更改的任何单元之前出现在DPR中。甚至可能要使其成为第一个单元,除非那里已经有其他必须成为第一个单元,例如替换内存管理器。

If you want to make sure it gets executed first, make a new unit, put your changes in that unit's initialization block, and then make sure it ends up in the DPR before any of the units that will depend on the changes. You might even want to make it the first unit, unless you have other "must be first" units there already, such as replacement memory managers.

这篇关于在Delphi中初始化单元之前添加代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆