我已经在Delphi语言中创建了一个IoC,它能够自动注册任何带有IocSingletonAttribute的类。
AutoRegister如下所示。
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;
然后,我创建了一个实现并将IocSingletonAttribute添加到其中。它看起来像这样
[IocSingleton(TypeInfo(IIocSingleton))]
TIocSingleton = class(TInterfacedObject, IIocSingleton)
procedure DoSomeWork;
end;
那么,现在来看程序的实际代码。如果我写下面的代码,IoC就不能工作。AutoRegister过程没有检测到TIocSingleton。
var
Ioc: TIocContainer;
Singleton: IIocSingleton;
begin
Ioc := TIocContainer.Create;
try
Ioc.AutoRegister;
Singleton := Ioc.Resolve<IIocSingleton>();
Singleton.DoSomeWork;
finally
Ioc.Free;
end;
end.
但是,如果我编写下面的代码,一切都会按预期运行。注意我是如何声明TIocSingleton类并使用它的。
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.
因此,基于此,我假设Delphi的编译器链接器在第一个示例中删除了TIocSingleton,因为它从未在应用程序的任何部分显式使用过。所以我的问题是,有没有可能为某个类打开编译器的“删除未使用的代码”功能?或者,如果我的问题不是链接器,那么有谁可以解释为什么第二个示例可以工作,而第一个不能呢?
发布于 2017-02-09 06:41:56
感谢Sebastian Z回答和奥古斯丁·奥尔图的评论。他们的两个回答都给了我一个最终的解决方案。不幸的是,STRONGLINKTYPES不可能只用于一个类,而且这个类需要以某种方式被引用。我决定不使用Augstin Ortu确切的建议,但我确实使用了这个概念。
在定义IoC的单元中,我输出以下空过程。
procedure IocReference(AClass: TClass);
implementation
procedure IocReference(AClass: TClass);
begin
end;
在创建要由IoC使用的类的类中,我添加了以下内容
initialization
IocReference(TIocSingleton);
end.
使用过程来防止链接器删除代码而不是仅仅调用类函数的原因,例如(TIocSingleton.ClassName)是因为它提供了更好的信息。如果另一个程序员阅读了代码,他们可以很好地猜测为什么会有这一行。
发布于 2017-02-06 05:38:16
将{$STRONGLINKTYPES ON}
指令添加到.dpr。那么这些类型就应该包括在内。但它肯定会毁掉你的应用程序,因为它不适用于单个类。
https://stackoverflow.com/questions/42054460
复制相似问题