mardi 30 avril 2019

How to find interface in assemblies? For plugin system

I am trying to create a plugin system but I get "Object reference not set to object" Error.

I have loaded each dll as a assembly then used Assembly.GetTypes(). I loop through my types to find my IPlugin interface. I then use Activator.CreateInstance() to create a new IPlugin for a reference and add it to my Plugins list.

           foreach(string dll in dllFiles) {
                AssemblyName an = AssemblyName.GetAssemblyName(dll);
                Assembly assembly = Assembly.Load(an);

                Type[] types = assembly.GetTypes();

                foreach(Type type in types) {
                    if(type.GetInterface(PluginType.FullName) != null) {
                        IPlugin plugin = Activator.CreateInstance(type) as IPlugin;
                        Plugins.Add(plugin);
                        Console.WriteLine("Added a plugin!");
                    }
                }
            }


I expect to be able to loop through my Plugins list and call the method "Do()" in each but I get a object reference not set to object.





Aucun commentaire:

Enregistrer un commentaire