问题描述
我要开发一个系统,以监测传感器信息,但许多传感器可能会在将来增加。
I have to develop a system to monitor sensor information, but many sensors might be added in the future.
这是说,这个想法是开发出将包括应用程序框架的系统。传感器(如他们每个人都有其通信和数据presentation特性)将被添加为插件到系统中。
That said, the idea would be to develop a system that would consist of the application skeleton. The sensors (as each of them has its communication and data presentation characteristics) would be added as plugins to the system.
我怎么会code这在C#?它是组件驱动发展的情况?我应该使用动态库?
How would I code this on C#? Is it a case of component-driven development? Should I use dynamic libraries?
推荐答案
有一个巨大的C#特设插件系统的数量。一种是使用C#中的 (在)。一般的做法是,主机应用程序发布与接口的组件。它通过枚举一个文件夹,并认为确定实现其接口并加载它们的类组件和实例化类。
There are a huge number of ad-hoc plug-in systems for C#. One is described in Plugin Architecture using C# (at The Code Project). The general approach is that the host application publishes an assembly with interfaces. It enumerates through a folder and finds assemblies that define a class that implement its interfaces and loads them and instantiates the classes.
在实践中你想要做的更多。这是最好的,如果主机应用程序定义了两个接口,一个IHost和IPlugIn。所述IHost接口提供一个插件可以订阅服务。该IPlugIn被构造服用IHost。
In practice you want to do more. It's best if the host application defines two interfaces, an IHost and an IPlugIn. The IHost interface provides services that a plug-in can subscribe to. The IPlugIn gets constructed taking an IHost.
要加载插件,你应该做更多的不是简单地得到一个插件。你应该列举所有的插件是可装载。构建他们每人。问他们是否可以运行。让他们的API导出到主机。让他们从主机导入的API。外挂插件应能询问其他插件的存在。
To load a plug-in, you should do more than simply get a plug-in. You should enumerate all plug-ins that are loadable. Construct them each. Ask them if they can run. Ask them to export APIs into the host. Ask them to import APIs from the host. Plug-ins should be able to ask about the existence of other plug-ins.
这种方式,插件可以通过提供多个API扩展应用程序。
This way, plug-ins can extend the application by offering more APIs.
插件应包括事件。这样的插件可以监视插件装载和卸载的过程。
PlugIns should include events. This way plug-ins can monitor the process of plug-ins loading and unloading.
在世界的尽头,你应该警告说,他们将离开插件。然后带他们出去。
At the end of the world, you should warn plug-ins that they're going to go away. Then take them out.
这将离开你,可以写在一个很小的框架,如果你想让它在插件中完全实现的应用程序。
This will leave you with an application that can be written in a tiny framework and implemented entirely in plug-ins if you want it to.
作为额外的奖励,你也应该让这个在插件文件夹,您解析快捷方式插件。这使您可以编写应用程序,并提供给别人。他们可以在自己的开发环境中编写一个插件,在应用程序的插件文件夹创建一个快捷方式到它,而不是担心每次编译后的部署。
As an added bonus, you should also make it so that in the plug-ins folder, you resolve shortcuts to plug-ins. This lets you write your application and deliver it to someone else. They can author a plug-in in their development environment, create a shortcut to it in the application's plug-ins folder and not have to worry about deploying after each compile.
这篇关于系统在C#插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!