Janus... No problem, but there are security risks.
http://www.geocities.com/jeff_louie/OOP/oop13.htm
// now dynamically load class DrawPlugIn.Triangle which
// implements interface MyInterface.IDrawable
Assembly assembly= null;
try
{
// tell app where to look for plugins
AppDomain.CurrentDomain.AppendPrivatePath("plugins ");
// get absolute path to our private assemblies
string path=
AppDomain.CurrentDomain.BaseDirectory+"plugins";
// create plugins folder if one does not exist
DirectoryInfo info= new DirectoryInfo(path);
if (!info.Exists){ info.Create();}
// discover all dlls in plugins folder
string[] dir= Directory.GetFiles(path,"*.dll");
// iterate over files in folder plugins
foreach (string s in dir)
{
string dll= Path.GetFileNameWithoutExtension(s);
assembly= Assembly.Load(dll); // in folder
plugins
Type[] types= assembly.GetTypes();
foreach(Type t in types)
{
Console.WriteLine("Type: {0}",t);
try
{
// only load if implements
IDrawable
// use fully qualified name!
if
(t.GetInterface("MyInterface.IDrawable")!= null)
//if
(typeof(IDrawable).IsAssignableFrom(t)) // safer
{
// dynamically load this
class
object obj=
Activator.CreateInstance(t);
drawableList.Add(obj);
// no need to cast
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
Regards,
Jeff
Just wondering, is it possible to make a folder with assemblies and
have the
application to read it in runtime?<
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!