gpt4 book ai didi

delphi - 为什么一个类需要调用一个空方法来注册它?

转载 作者:行者123 更新时间:2023-12-03 15:46:34 24 4
gpt4 key购买 nike

我正在评估 Delphi 的现有依赖注入(inject)库,并决定使用这个 delphidicontainer由于其简单性 - 它只需要一个 unit file !

但是,有一件事情我不明白——在 this example file 中在底部:

initialization
//with those lines the linked will include the actual code for the services
TCSVDataService.Register;
TPaddedDataService.Register;

这些Register方法是空的,但是,如果我在初始化部分注释掉这两行,DIContainer.Get('dataservice') as TDataService将会失败。

为什么?这些 方法有什么用?

最佳答案

Delphi编译器很聪明,在编译/链接代码时会尝试消除未使用的代码,因此从编译器的角度来看,示例中的实现类(例如,TCSVDataService)不是在程序中的任何地方使用并将被消除,因此这些空方法调用可以防止这种情况发生。

关于delphi - 为什么一个类需要调用一个空方法来注册它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179606/

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