在 CAL 中,如何获取模块实例的引用?
我有这个使用 CAL 的应用程序。 所有模块都派生自一个特殊的类 ModuleBase
,该类具有一个抽象方法,例如在每个模块中实现的 ApplySecurity
。
好的,我在引导程序中加载模块,在调用 bootstrapper.Run()
后,我想访问所有已加载的模块并调用此 ApplySecurity
方法。
我尝试了这个:
IModuleCatalog moduleCatalog = this.Container.Resolve<IModuleCatalog>();
moduleCatalog.Modules.ToList().ForEach(m =>
{
(this.Container.Resolve(Type.GetType(m.ModuleType, false, false))
as ModuleBase).ApplySecurity(); //^^^ this is making new instances!!
});
但这正在创建模块的新实例,并且我想引用已经初始化的实例。
我希望我已经说得足够清楚了,
特奥多。
I have this application using CAL.
All the modules derive from a special class, ModuleBase
, which has an abstract method, say ApplySecurity
implemented in each one of them.
OK I load the modules in the bootstrapper, and after i call bootstrapper.Run()
, i want to access all the modules that were loaded and call this ApplySecurity
method.
I tried this:
IModuleCatalog moduleCatalog = this.Container.Resolve<IModuleCatalog>();
moduleCatalog.Modules.ToList().ForEach(m =>
{
(this.Container.Resolve(Type.GetType(m.ModuleType, false, false))
as ModuleBase).ApplySecurity(); //^^^ this is making new instances!!
});
but this is making new instances of modules, and i want to reference the ones that were already initialized.
I hope i have been clear enough,
Teodor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题在于,当模块注册到容器时,它没有注册为特定实例,因此当您请求解析时,会创建一个新实例。 我查看了 CAL 源代码,注册发生在一组复杂的方法调用中间,因此我怀疑您是否能够明智地提供自己的实现。
这意味着您需要在其他地方注册模块的实例。 我想到的方法是创建某种模块实例目录,并添加对每个模块的 initilize 方法的调用以向其注册。
在您的 Bootstrapper CreateShell():
在每个模块的 Initilize(): 中,
然后将问题中的代码替换为:
我对此并不太满意,因为它感觉有点模糊,但它应该可以满足您的需求。
It looks like the problem is that when the Module is registered to the container it isn't registered as a specific instance, so when you request the resolve a new instance is created. I've had a look through the CAL source code and the registering happens in the middle of a convoluted set of method calls, so I doubt you'll be able to sensibly supply your own implementation.
This means you'll need to register the instance of your module somewhere else. The way to do this off the top of my head is to create some kind of module instance catalog and add a call to each of the module's initilize method to register with it.
In your Bootstrapper CreateShell():
In each module's Initilize():
Then replace the code in your question with:
I'm not overly happy with this as it feels like a bit of a fudge, but it should work for what you want.