5
Answers

Dynamic Assembly Loading

Wes

Wes

16y
4.2k
1
I'm trying to extend my CMS with a plugin interface to make extensibility a little easier to manage.  I created 2 classes:  an abstract base class (baseModule) containing a single abstract method Init() and a class that inherits from it (TestModule) and throws an exception when Init is called.  In my Engine, I'm doing the following:

            if (!module_dir.Exists) throw new Exception("Modules directory does not exist.");
            foreach (FileInfo f in module_dir.GetFiles("*.dll"))
            {
                Type t = Assembly.LoadFile(f.FullName).GetType();
                baseModule m = Activator.CreateInstance(t) as baseModule;
                if (m == null)
                {
                    throw new Exception("dll is not of type IModule");
                }
                module_list.Add(m);
            }

Unfortunately when I hit Activator.CreateInstance, I get the error :

"No parameterless constructor defined for this object."

Any ideas what I'm doing wrong?

Answers (5)