何时使用ExternallyControlledLifetimeManager?
我正在浏览 Unity 中可用的不同 LifetimeManager,并且想知道我们什么时候会使用ExternallyControlledLifetimeManager?有人能给我一个现实生活中的例子吗? MSDN 文档说“LifetimeManager 持有对其托管实例的弱引用”。
开发人员如何管理实例以及何时应该使用这样的生命周期管理器?您对如何以及何时使用它的想法对于我和其他人来说非常有帮助。 谢谢!
I was going through different LifetimeManagers available in Unity and was wondering when will we use ExternallyControlledLifetimeManager? Can somebody give me an real life example? The MSDN doc says "A LifetimeManager that holds a weak reference to it's managed instance"..
How can the developer manage the instance and when should he go for such a lifetime manager? Your thoughts on how and when you used it would be great for me and others to understand.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要注册由其他框架或库创建的对象并且您无法控制其生命周期(例如第三方库提供的单例),那么它可能会很有用。
因为容器只保留弱引用,所以如果没有任何强引用,GC 将删除该单例。 IMO,仅当您无法管理实例的生命周期时才应使用它。
It could be useful if you're going to register an object created by other frameworks or library and you haven't the control of its life, for example a singleton provided by a third-party library.
Because the container keeps only a weak reference, the GC will delete that singleton if there aren't any strong reference. IMO, you should use it only if you can't manage the life time of the instance.