在 C# 应用程序中获取插件或扩展功能的步骤是什么?
我已经在 Win32 & 中完成了插件架构。 C/C++ 已有多年历史,使用 LoadLibrary、GetProcAddress 等从 DLL 动态加载扩展点。
现在是 C# 的时代了。相应的步骤是什么 - 动态加载程序集?或者它是一个完全不同的模式?
I've done plugin architectures in Win32 & C/C++ for years, with extension points dynamically loaded from DLLs with LoadLibrary
, GetProcAddress
, etc.
Now the time has come to C#. What are the corresponding steps there - dynamically load an assembly? Or is it a completely different schema?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,您可以查看您期望插件的位置,加载程序集并查找某些类。通常插件通过扩展某种插件基类或实现接口来宣传自己。
另一种选择是 MEF,它也将成为 .NET 4 的一部分,但预览版适用于当前版本.NET 框架也是如此。
Generally, you can look around where you expect plugins, load the assemblies and look for certain classes. Usually plugins advertise themselves by extending some sort of plugin base class or implementing an interface.
Another option would be MEF which will also be part of .NET 4 but the preview releases work on the current .NET framework, too.
您可以使用 System.Addin 命名空间中的类。请参阅此讨论:
在 MEF 和 MAF 之间进行选择 (System.AddIn)
这里也是一个演示: AddIn 启用的应用程序
另一种解决方案是使用 Mono.AddIn 看起来相当强大。
You can use classes from System.Addin namespace. See this discussion:
Choosing between MEF and MAF (System.AddIn)
Here is a demo too: AddIn Enabled Applications
Another solution is to use Mono.AddIn which seems quite powerful.
在.Net应用程序中,我们可以使用AppDomain和AppDomain.CurrentAppDomain将程序集动态加载到我们的应用程序中。问题是,一旦程序集加载到AppDomain中,您就可以卸载程序集。有一个解决方法可以解决此问题,您可以加载这些程序集但是这种方法非常复杂,因为在两个不同 AppDomain 中的两个程序集之间传递对象并不那么容易。
In .Net applications we can use AppDomain and AppDomain.CurrentAppDomain to load assemblies dynamically to our application .The problem is that you can unload an assembly once it has been loaded to a AppDomain.There's a workaround to solve this problem that you can load these kind of assemblies in a different AppDomain and unload it whenever you don't want those assemblies.but this approach is very compicated because passing objects between two assemblies in two different AppDomain it's not that easy.