System with plugins in C#

后端 未结 7 1931
臣服心动
臣服心动 2020-12-09 13:08

I have to develop a system to monitor sensor information, but many sensors might be added in the future.

That said, the idea would be to develop a system that would

相关标签:
7条回答
  • 2020-12-09 13:13

    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.

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-09 13:13

    Each sensor should implement a standard interface so that routines that handle lists of sensors can treat them in a standard manner. Include a ID field in the interface that is unique too each type of sensor so you can handle special cases.

    Look at the Reflection API to learn how to scan a directory of .NET Assemblies and look inside them.

    Each assembly should have a factory class that it's job is to return a list of sensors that are in that assembly. I recommend that you make it a subroutine not a function and passes it a list that that it appends too. SensorDLL1 appends 4 sensors to the emptylist, SensorDLL2 appends 8 sensor to the list which now has 12 sensors and so on. This approach is the most flexible in the long run.

    You will either have to make up a naming convention to find the factory class or use an attribute. Note I don't recommend just scanning the assembly for everything that implements your sensor interface as you could have code inside the factory that controls which sensors are available. This is useful for licensing.

    0 讨论(0)
  • 2020-12-09 13:14

    We once made a plug-in system in a school project of ours in 2006, Socio. You can find the code here and here.

    The basic lesson learned was that it is very simply to dynamically load code in C#. If you just have a plugin DLL and an application which adheres to an interface of yours and links against a common DLL in which that interface exists, it just works™.

    In essence, it is what plinth described in his answer.

    0 讨论(0)
  • 2020-12-09 13:16

    Depending upon the Sensors themselves, this sounds like you would need to define a single interface which all Sensors will implement. Your main "application skeleton" will then work against the ISensor interface and need not concern itself with the concrete implementations of each of the Sensor classes/objects/components.

    Whether each Sensor is simply a class within the same project, or a separate assembly is up to you, although if they are separate assemblies, you'd need a way to load these assemblies dynamically.

    Some references which may help here are:

    Command Pattern Design Pattern: - http://en.wikipedia.org/wiki/Command_pattern

    Observer Pattern Design Pattern: - http://en.wikipedia.org/wiki/Observer_pattern

    Dynamically loading assemblies: - http://www.divil.co.uk/net/articles/plugins/plugins.asp

    Hope this helps.

    0 讨论(0)
  • 2020-12-09 13:25

    Take a look at:

    Composite UI Application Block and Smart Client Software Factory

    0 讨论(0)
  • 2020-12-09 13:32

    Its a very old post but still i thought it would be useful for someone to appPress.in where in we have developed a framework with plugin functionality. here we allow plugin to modify the UI of core application Horizontally and Vertically, add its own Pages, hook into events like Init, OnClick and OnChange.

    0 讨论(0)
提交回复
热议问题