I've been having some major problems with executing .NET assemblies via C#. I want to invoke a static method (Main), from a static class. I had it working in the current domain, but apparently I can't unload the assemblies from there.
Firstly, I tried creating a new AppDomain:
AppDomain domain = AppDomain.CreateDomain("PluginDomain");
Since my main issue is with resolving the assemblies I'm trying to load, I tried to hook the AssemblyResolve event:
domain.AssemblyResolve += new ResolveEventHandler(this.OnAssemblyResolve);
However, I get an error about serialization with this (ie. the this class).
I have successfully loaded an assembly file which happens to be located in the BaseDirectory by simply using:
domain.Load("Assembly1");
But the problematic assembly is located within two subdirectories of the BaseDirectory, ie. BaseDirectory\dir1\dir2\Assembly2.dll - I get a FileNotFoundException when using domain.Load() with a string assembly name (with a PrivateBinPath setup to the correct subdirectory location) and also when using domain.Load() with the assembly bytes (from File.ReadAllBytes()).
I am aware that AppDomain.Load() is deprectated, but it seemed like the only useable method to me, considering that I don't want to force the class containing a static method to be non-static and instantiatable.
Anyway, what can I do to ensure the assembly file is loaded into the new AppDomain correctly?
My main objective with this, is to dynamically execute a method from an assembly which can make calls into my own program, yet the assembly must also be unloaded completely when necessary. It's intended to be somewhat of a plugin system.