Hi Dave,
After fidling with some code I think I got a workable solution, although
it
may not be the most elegant.
The code below assumes there is a Data.dll in a nearby folder, and loads
it
into a subdomain. The trick is that AppDomain.Load requires assembly
names
or similar instead of the exact file name, so you may need to load it
using
Assembly.LoadFile (which loads the assembly into CurrentDomain) and obtain
its FullName first. Having the assembly name you can tell the sub domain
to
look anywhere on the disk for the file, and load it.
protected override void OnLoad(EventArgs e)
{
string assemblyName =
@"C:\Projects\VS2008\WinTest\Data\bin\Debug\Data.dll";
string assemblyPath = @"..\..\Data\bin\Debug\";
AppDomain subDomain = AppDomain.CreateDomain("subDomain",
null,
AppDomain.CurrentDomain.BaseDirectory,
assemblyPath,
false);
subDomain.Load(assemblyName);
Assembly[] assemblies = subDomain.GetAssemblies();
Assembly myAssembly = null;
foreach (Assembly assembly in assemblies)
if (assembly.FullName == assemblyName)
myAssembly = assembly;
Type myType = myAssembly.GetType("Data.SomeObject");
object myObject = myAssembly.CreateInstance("Data.SomeObject");
string result = (string)myType.InvokeMember("SomeMethod",
BindingFlags.InvokeMethod,
null,
myObject, null);
AppDomain.Unload(subDomain);
}
Once the subdomain is unloaded the assembly file is no longer locked even
though the CurrentDomain is still running.
--
Happy Coding!
Morten Wennevik [C# MVP]
DaveL said:
Im looking for some good links
to show me how to load and unload dlls at runtime
specifically
I would like to load a dll at runtime in a secondarydomain
exec a static method then unload the domain
Thanks
DaveL