使用 MEF,我可以在导出任何类型之前运行方法吗?
MEF 中有没有办法让我的方法之一在导出任何类型之前运行?
我在程序集 A 中使用 MEF,而我使用的某些类型在另一个程序集 (B) 中。程序集 B 存储在程序集 A 的资源中(代码混淆的一部分),因此我需要处理 AppDomain.AssemblyResolve 事件,以便我可以加载程序集 B。
所以我希望能够告诉 MEF在导出任何类型之前运行方法 X(它将处理该事件)。目前,我需要向导出类型的所有构造函数添加代码,并避免在错误的位置使用程序集 B 中的任何类型(即,如果它们在 AssemblyResolve
事件之前得到解析)处理)。
Is there a way in MEF to have one of my methods run before any types are exported?
I'm using MEF in assembly A, and some of the types I'm using are in another assembly (B). Assembly B is stored inside the resources of assembly A (part of code obfuscation), so I need to handle AppDomain.AssemblyResolve
event so I can load assembly B.
So I'd like to be able tell MEF to run method X (which will handle that event) before exporting any types. At the moment, I need to add code to all the constructors of the exported types, and avoid using any types from assembly B in the wrong place (i.e. if they would be resolved before the AssemblyResolve
event has been handled).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您控制主机,则可以在进行任何组合之前运行代码。
听起来您正尝试在扩展中执行此操作,但主机对混淆一无所知。 MEF 没有任何明确的内容来支持这一点。您可以执行诸如对所有导出使用自定义导出属性之类的操作,并将代码放入导出属性的构造函数中,以确保加载程序集 B。这违背了导出属性应该做的事情——它们应该只提供元数据,而不是实际运行执行任何操作的代码。不过,它可能对你有用。
另一件让我担心的事情是,作为扩展,您正在连接到 AppDomain.AssemblyResolve。我认为只会使用该事件的一个处理程序,因此如果主机或任何其他扩展想要挂钩此行为,它并不适合所有人。
If you control the host, you can run code before you do any composition.
It sounds like you are trying to do this in an extension and the host doesn't have any knowledge of the obfuscation, though. MEF doesn't have anything explicitly to support this. You could do something like use a custom export attribute for all of your exports, and put code in the export attribute's constructor that ensures that assembly B is loaded. This is going against what Export attributes are supposed to do-- they are supposed to just supply metadata and not actually run code which does anything. However, it might work for you.
Another thing that worries me is that as an extension you are hooking into AppDomain.AssemblyResolve. I think that only one handler for the event will be used, so if the host or any other extensions want to hook into this behavior, it won't work for everyone.