创建一个Delphi IoC.如何禁用Delphi的链接器以删除未使用的类 [英] Creating a Delphi IoC. How to disable Delphi's linker from removing unused classes

查看:55
本文介绍了创建一个Delphi IoC.如何禁用Delphi的链接器以删除未使用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在delphi中创建了一个IoC,它能够自动向其注册任何具有IocSingletonAttribute的类.

I've created a IoC in delphi with the ability to automatically register any classes that have a IocSingletonAttribute to it.

自动注册如下所示.

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添加到其中.看起来像这样

I then create a implementation and add the IocSingletonAttribute to it. It looks like this

[IocSingleton(TypeInfo(IIocSingleton))]
TIocSingleton = class(TInterfacedObject, IIocSingleton)
  procedure DoSomeWork;
end;

所以,现在到程序的实际代码了.如果我在IoC下方编写代码,则无法正常工作.自动注册过程未使用TIocSingleton.

So, now to the actual code of the program. If I write the code below the IoC doesn't work. The AutoRegister procedure didn't 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.

但是,如果我在下面编写代码,则一切正常.注意我如何声明TIocSingleton类并使用它.

But if I write the code below instead everything works as expected. Notice how i have declared TIocSingleton class and have 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.

因此,基于此,我假设在第一个示例中,Delphi的编译器链接器正在删除TIocSingleton,因为它从未在应用程序的任何部分中显式使用.所以我的问题是,是否可以为某个类打开编译器的删除未使用的代码"功能?或者,如果我的问题不是链接器,那么谁能说明第二个示例为何起作用,而第一个示例无效?

So based on this, I'm assuming Delphi's compiler linker is removing TIocSingleton in the first example because it was never explicitly used in any part of the application. So my question is, is it possible to turn of the compiler's 'remove unused code' feature for a certain class ? Or if my problem isn't the linker, can anyone shed light on why the second example works but not the first ?

推荐答案

感谢塞巴斯蒂安Z对Agustin Ortu的回答.他们的两个回答都使我得到了最终解决方案.不幸的是,不可能仅对一个类使用STRONGLINKTYPES,并且需要以某种方式引用该类.我决定不使用精确的建议,但我确实使用了这个概念.

Thanks to Sebastian Z answer for and for Agustin Ortu comment. Both their responses got me to a final solution. It's not possible to use STRONGLINKTYPES for just one class, unfortunately, and the class needs to be referenced somehow. I decided not to use Augstin Ortu exact suggestion but I did use the concept.

在定义了IoC的单元中,我输出以下空过程.

In the unit where IoC is defined I output the following empty procedure.

procedure IocReference(AClass: TClass);

implementation

procedure IocReference(AClass: TClass);
begin
end; 

然后在创建要由IoC使用的类的类中,添加以下内容

And in the class that creates a class to be used by IoC I add the following

initialization
  IocReference(TIocSingleton);
end.

使用过程防止链接程序删除代码而不仅仅是调用类函数(例如(TIocSingleton.ClassName))的原因是它提供了更好的信息.如果另一位程序员阅读了该代码,他们将可以很好地猜测该行为何存在.

The reason to use a procedure to keep the linker from removing the code instead of just calling a class function, eg (TIocSingleton.ClassName) is that it provided better information. If another programmer reads the code they can take a good guess as to why that line is there.

这篇关于创建一个Delphi IoC.如何禁用Delphi的链接器以删除未使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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