Using assemblies in runtime?

J

Janus Knudsen

Hello..

Just wondering, is it possible to make a folder with assemblies and have the
application to read it in runtime?

Possible use: Could be menues, if the menu assembly exists in the folder
then it's added to the gui !
Reason: licensing


Kind regards
 
J

Jeff Louie

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?<
 
G

Guest

It is possible,you can use the static method Load of the Assembly to load the assembly to the AppDomain and use reflection to access the metadata at runtime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top