几年前我们做了类似的事情。我们是如何处理它的,我们创建了几个接口,这些接口由一个PluginManager和插件实现。
插件管理器实现了一个类似于以下内容的界面:
''' <summary>
'''The IPluginManager interface is implemented by whatever component manages your gui
'''It provides a means for plugins to access GUI elements of the application
''' </summary>
Public Interface IPluginManager
''' <summary>
'''The MDIForm property allows the plugin to display itself
'''inside of the application's main MDI form (ie. plugin.form.mdiparent=mdiform)
''' </summary>
ReadOnly Property MDIForm() As Form
ReadOnly Property Toolbar() As ToolBar
''' <summary>
'''Allows the plugin to request that the application's main menu be updated
''' </summary>
''' <param name="Menu">The menu to add to the main menu</param>
Sub UpdateMainMenu(ByVal Menu As Menu)
''' <summary>
'''Allows the plugin to request that the application's workspace toolbar be updated
''' </summary>
''' <param name="Buttons">the collection of toolbar buttons to add to the toolbar</param>
Sub UpdateToolbarButtons(ByVal Buttons As ToolBar.ToolBarButtonCollection)
End Interface
插件实现了这个接口:
''' <summary>
'''The IPlugin interface is implemented by all plugins
'''It provides a standardized means for the pluginmanager
'''to communicate with a plugin, without knowing the plugin explicitly
''' </summary>
Public Interface IPlugin
''' <summary>
'''Allows the plugin to be intialized first before it asked to run
''' </summary>
Sub Initialize()
''' <summary>
'''Allows the pluginmanager to register itself with the plugin
'''so the plugin can listen to events from the pluginmanager
''' </summary>
''' <param name="PluginManager">A plugin manager that implements the IPluginManager interface</param>
Sub RegisterPluginManager(ByVal PluginManager As IPluginManager)
''' <summary>
'''Requests the plugin to run its functionality
''' </summary>
Sub Run()
End Interface
应用程序启动后,PluginManager会找到所有可用的插件(听起来你已经有了这个功能),然后实例化每个插件,并向每个插件注册自己。然后,PluginManager初始化并运行插件。