How can I create a plugin mechanism that calls functions only when the plugin is available?

不羁的心 提交于 2020-01-15 06:54:12

问题


Sorry if I am not clear enough, I've had a hard time writing this question.

I downloaded an open source software. I would like to expand the functionalities so I would like to create modules that encapsulates the functionality these modules would be .dll files. I would like to have one completely independent from another: if I set a key to true in the config file and if the DLL is present on the folder, the plugin should be loaded.

The problem is: how can I make the call for the plugin dynamically (only call of the plugin is applied)?

If I reference the plugin classes directly, I would have to reference the plugin dll, but I want to be able to run the core software without the plugin. Is there any design pattern or other mechanism that would allow me to load and use the DLL only if the plugin is applied and still be possible to run the core software without the plugin?


回答1:


There are various ways to achieve this and I will describe one simple solution here.

Make a common interface that each plugin must implement in order to be integrated with core application. Here is an example:

// Interface which plugins must implement
public interface IPlugin
{
  void DoSomething(int Data);
}

// Custom plugin which implements interface
public class Plugin : IPlugin
{
  public void DoSomething(int Data)
  {
    // Do something
  }
}

To actually load your plugin from dll, you will need to use reflection, for example:

// Load plugin dll and create plugin instance
var a = Assembly.LoadFrom("MyCustomPlugin.dll");
var t = a.GetType("MyCustomPlugin.Plugin");
var p = (IPlugin)Activator.CreateInstance(t);

// Use plugin instance later
p.DoSomething(123);

You can use some kind of naming convention for your plugin assemblies and classes so that you can load them easily.




回答2:


You can use MEF.

The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.

Here is programming guide.




回答3:


Plugins or DLLs in .NET jargon are called assemblies. Check out the Assemply.Load method, and also this guide in msdn.




回答4:


The System.Reflection namespace provides many tools that will help you with this scenario.

You can

  • inspect assemblies (DLL files) to examine the objects inside them,
  • find the types that you are looking for (specific classes, classes which implement specific interfaces, etc)
  • create new instances of those classes, and
  • invoke methods and access properties of those classes.

Typically you would write a class in the extension which does some work, create a method (e.g. DoWork()), and then invoke that method dynamically.

The MEF mentioned in this question does exactly this, just with a lot more framework.



来源:https://stackoverflow.com/questions/14926287/how-can-i-create-a-plugin-mechanism-that-calls-functions-only-when-the-plugin-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!