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

Edw*_*Yip 3 delphi

我正在为Delphi评估现有的依赖注入库,并且由于其简单性而确定了这个delphidicontainer - 它只需要一个单元文件!

但是,有一件事我不明白 - 在这个示例文件的底部:

initialization
//with those lines the linked will include the actual code for the services
TCSVDataService.Register;
TPaddedDataService.Register;
Run Code Online (Sandbox Code Playgroud)

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

为什么?这些方法是什么?

Edw*_*Yip 7

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