MEF 运行时的导入和导出如何工作?
我开始学习 MEF,其中一件重要的事情是我可以使用 Export 属性标记某些项目(类、属性、方法),这样,想要使用它的人就可以在实例变量上创建 Import 属性并使用它。这种映射是如何发生的以及何时发生?导入是按需延迟发生还是所有组合都在启动时发生?抱歉这个无知的问题,我正在尝试理解流程。
I am starting to learn, MEF and one important thing in it is that I can mark some item (class, propety,method) with Export attribute so that, who ever wants use it will create Import attribute on an instance varaible and use it. How does this mapping happen and when does it happen? Is the import happen lazily on demand or all the composition happen at the start up? Sorry for the ignorant question, I am trying to understand the flow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它发生在一个称为“组合”的阶段。首先,您创建一个容器并将所有可能的部件源加载到其中,然后
Compose
它。当您进行组合时,它会解析所有依赖项,如果无法正确解析所有依赖项,则会引发异常。一般来说,您的部件会在组合期间实例化(如果您在部件类的构造函数中设置断点,您将在调用
Compose()
期间看到断点命中)。但是,如果您使用Lazy
作为导入类型(假设您将部件导出为T
类型),则可以以直接的方式覆盖此设置。要了解组合的工作原理,请查看
Compose()
方法 此处。It happens in a phase called "Composition". First you create a container and load all your possible sources of parts into it, and then you
Compose
it. When you do the composition, it resolves all the dependencies and throws an exception if it can't resolve them all properly.In general, your parts get instantiated during composition (and if you set a break point in the constructor of your part classes, you will see the break point hit during your call to
Compose()
). However, you can override this in a straightforward way if you useLazy<T>
as the type of your import (assuming you exported your part as typeT
).To see how the composition works, take a look at the
Compose()
method here.