C#插件功能的模式/方法 [英] Pattern/Method for C# Plugin Functionality

查看:328
本文介绍了C#插件功能的模式/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种新产品,最终将根据个人客户的需求为其提供不同的功能.因此,我希望实现具有可以部署的插件的功能,并且核心应用程序将检测到这些插件并进行相应的更改.最重要的是,该产品很可能将针对不支持System.Addin或MEF可能性的.Net早期版本.

I'm working on a fresh new product that eventually will have different functionality built up around it based on individual customer requirements. Therefore, I'd like to implement the ability to have plugins that I can deploy and the core application will detect the plugins and change accordingly. The kicker is that the product will most likely be targeting earlier versions of .Net that do not support System.Addin or the possibility of MEF.

也就是说,哪种编程模式/方法可以很好地支持C#插件?我想完全远离第三方框架,尽管可以随意命名.理想情况下,我希望能够插入一个插件,使其能够修改主菜单和/或上下文菜单和/或提供新服务.在Java中,可以通过扩展点很好地处理它.我基本上会坚持写我自己的扩展点管理器吗?

That said, which patterns/methods of programming lend themselves well to supporting plugins in C#? I'd like to stay away from third party frameworks altogether, although feel free to name one. Ideally, I would like to have the ability to drop in a plugin have it modify the main menu and/or a context menu and/or provide a new service. In Java this was handled nicely via extension points. Am I basically stuck to write my own extension point manager?

推荐答案

您想要查看System.Reflection命名空间来帮助您完成此任务.一种方法是为您的插件类创建一个通用接口(查找策略模式),然后在运行时使用反射来查看给定文件夹(可能是exe所在的文件夹或特殊的外接程序文件夹)中的所有dll,并记下包含该特定接口的类.具体而言,保留类型的字典(具有特殊接口的类型将其指定为插件类).一旦获得了包含将它们指定为插件的接口的那些类型的列表,就可以使用System.Activator类随意动态地实例化它们.在应用程序启动时对加载项类进行初始探测,然后使用抽象工厂根据类型或某些其他元数据来加载插件,这些元数据可以使用.net属性归因于该类.

You want to look at the System.Reflection namespace to aid you in this task. One approach would be to create a common interface for your plugin classes (lookup strategy pattern) and then use reflection at runtime to look at all the dll's in a given folder (maybe where the exe is or a special add-in folder) making note of the classes that contain that particular interface. In specific keep a dictionary of the types (the ones with your special interface that designates it as a plugin class). Once you have a list of those types that contain the interface designating them as a plugin you can dynamically instantiate them at will using the System.Activator class. Do the initial probing for the add-in classes at application startup and then use a abstract factory to load the plugin based on type or some other metadata that can be attributed to the class using .net attributes.

如果您需要特定的代码示例,我可以为您提供帮助,但是,如果您打算构建,则应该花些时间熟悉System.Reflection,System.Activator和.net属性.一个强大而灵活的加载项系统.

If you need specific code examples I can do this for you but, you should take the time to become familiar with System.Reflection, System.Activator, and .net attributes if you intend to build a robust and flexible add-in system.

享受!

这篇关于C#插件功能的模式/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆