Quote:
Originally Posted by Huggz
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:
Code:
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?
it seems that the variable t refer to "System.Reflection.Assembly" not to the dll you want to instantiate. below is a simple sample code about dynamic loading, you need to modify it to fit in your application.
Code:
private void LoadAssembly()
{
Assembly a = Assembly.LoadFile(filename);
System.Type[] sts = a.GetTypes();
foreach (System.Type st in sts)
{
Object o = Activator.CreateInstance(st);
if (o != null)
{
Console.WriteLine(st.FullName + " Instantiated\n");
//proceed with type conversion here
}
else
{
Console.WriteLine(st.FullName + " NOT Instantiated\n");
}
}
}
hope this help