I created IoC in delphi with the ability to automatically register any classes that have an IocSingletonAttribute.
The auto registry is as follows.
procedure TIocContainer.AutoRegister; var ctx: TRttiContext; rType: TRttiType; attr: TCustomAttribute; &Type: PTypeInfo; begin ctx := TRttiContext.Create; for rType in ctx.GetTypes do Begin for attr in rType.GetAttributes do Begin if TypeInfo(IocSingletonAttribute) = attr.ClassInfo then Begin &Type := IocSingletonAttribute(attr).&Type; RegisterType(&Type, rType.Handle, True); End; End; End; end;
Then I create an implementation and add IocSingletonAttribute to it. Looks like this
[IocSingleton(TypeInfo(IIocSingleton))] TIocSingleton = class(TInterfacedObject, IIocSingleton) procedure DoSomeWork; end;
So, now to the actual program code. If I write the code below, IoC does not work. AutoRegister procedure did not pick up TIocSingleton.
var Ioc: TIocContainer; Singleton: IIocSingleton; begin Ioc := TIocContainer.Create; try Ioc.AutoRegister; Singleton := Ioc.Resolve<IIocSingleton>(); Singleton.DoSomeWork; finally Ioc.Free; end; end.
But if I write the code below, everything will work as expected. Notice how I declared the TIocSingleton class and used it.
var Ioc: TIocContainer; Singleton: IIocSingleton; ASingleton: TIocSingleton; begin Ioc := TIocContainer.Create; ASingleton := TIocSingleton.Create; try Ioc.AutoRegister; Singleton := Ioc.Resolve<IIocSingleton>(); Singleton.DoSomeWork; finally Singleton.Free; Ioc.Free; end; end.
Therefore, based on this, I assume that the Delphi compiler linker removes the TIocSingleton in the first example, because it was never used explicitly in any part of the application. So my question is: is it possible to enable the "delete unused code" compiler function for a particular class? Or if my problem is not the linker, can someone shed some light on why the second example works, but not the first?