1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
public static void Load()
{
//Je crée l'AppDomain pour isoler mon plugin
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
//Important :Ici je dis que l'AppDomain que je vais créer, pourra
// chercher des assembly dans le sous repertoire "plugins"
setup.PrivateBinPath = "plugins";
AppDomain domaine = AppDomain.CreateDomain(
"Domaine Plugins",
null,
setup);
domaine.UnhandledException +=
new UnhandledExceptionEventHandler(domaine_UnhandledException);
//Je fais un ptit test pour la demo
if (!File.Exists("./plugins/plugin.dll"))
{
Console.WriteLine("Plugin absent");
return;
}
try
{
IPlugin plugin = (IPlugin)domaine.CreateInstanceFromAndUnwrap("plugin.dll", "Plugin.Plugin");
Console.WriteLine("appelle son Run");
plugin.Run();
}
catch(Exception err)
{
Console.WriteLine("Exception dans l'AppDomain par defaut:\r\n{0}",err.ToString());
}
}
static void domaine_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("La SandBox a levé une exception innatendue :(\r\n{1}", e.ExceptionObject.ToString());
} |
Partager