1

I'm trying to make a plugin system for my application so people can develop add-ons. This is a new concept to me. After a few nights of studying, I got the system for loading down. Now I'm struggling with how do I make my interface allow plugin developers to extend the application?

Simple example that would set me on the right track

I have a global string

string Filters = "csv files (*.csv)|*.csv";

Now how would I allow for plugin developers to extend this string to allow all files which would look like this

string Filters = "csv files (*.csv)|*.csv|All Files (*.*)|*.*";

My current plugin interface looks like this

namespace CSV_Analyzer_Pro.Core.PluginSystem {
    public interface IPlugin {
        string Name { get; }
        string Version { get; }
        string Description { get; }
        void Action();
    }
}

When the application is loaded all plugins contained in the plugin folder are loaded like this

    PluginLoader loader = new PluginLoader();
    loader.LoadPlugins();
    loader.Init();
}catch(Exception exc) {
    Console.WriteLine(string.Format("Plugins couldnt be loaded: {0}", exc.Message));
}

Once the plugins are loaded you will notice loader.Init() is called which calls the Action() of each plugin

public void Init() {
    if(Plugins != null) {
        Plugins.ForEach(plugin => plugin.Action());
    }
}

So with all this set in place, how do I begin allowing plugins to extend the functionality?

My main goal is for plugin developers to be able to add additional functionality to the application as well as modify existing functionality the issue. I have the most trouble with is how do I get all this information dynamically?

I realize that I should be asking the plugin for information, but I am struggling with the concept of "does this plugin exist?" I really dont understand the concepts behind a plugin system to allow the "community" to extend the application in anyway they see fit

For example, you have the base application with a menustrip. How would they add a new item to the menustrip? Would I have to hard code everything that is allowed to be extended through plugins? I would prefer not. I would like to allow total extendability without much interference on the dev side.

I did take a look at MEF and MAF but the documentation is really confusing and all research I did basically are just tutorials on how to find and load plugins and they create a simple console application. Nowhere does anyone go over how to allow plugin developers to extend the application through plugins.

I realize this is a big task and one not easily accomplished but I have already laid down the foundation for loading and registering plugins now I just need to know how I make this fit how I need it to.

Also here is the PluginLoader class in case it helps put things into context

namespace CSV_Analyzer_Pro.Core.PluginSystem {
    public class PluginLoader {
        public static List<IPlugin> Plugins { set; get; }

        public void LoadPlugins() {
            Plugins = new List<IPlugin>();

            if (Directory.Exists(Constants.PluginFolder)) {
                string[] files = Directory.GetFiles(Constants.PluginFolder);
                foreach(string file in files) {
                    if (file.EndsWith(".dll")) {
                        Assembly.LoadFile(Path.GetFullPath(file));
                    }
                }
            }

            Type interfaceType = typeof(IPlugin);

            Type[] types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass).ToArray();

            foreach(Type type in types) {
                Plugins.Add((IPlugin)Activator.CreateInstance(type));
            }
        }

        public void Init() {
            if(Plugins != null) {
                Plugins.ForEach(plugin => plugin.Action());
            }
        }
    }
}
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
FlamingGenius
  • 216
  • 3
  • 22

0 Answers0