I\'m looking for a good implementation of hot-swapping done in .NET. The things I need are:
You can provide a custom event handler for AssemblyResolve
by calling newAppDomain()
below. Supply your directory so AppDomain
looks there. When loading a Type, use function loadFromAppDomain()
to return it. This should allow you to copy new dlls to C:\dlls
at runtime and reload from there. (Forgive me, I translated this from my VB source to C# according to your tag.)
String dllFolder = "C:\\dlls";
public void newAppDomain()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(assemblyResolve);
}
private static Assembly assemblyResolve(Object sender, ResolveEventArgs args){
String assemblyPath = Path.Combine(dllFolder, new AssemblyName(args.Name).Name + ".dll");
if(!File.Exists(assemblyPath))
{
return null;
}
else
{
return Assembly.LoadFrom(assemblyPath);
}
}
private Type loadFromAppDomain(String className)
{
Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();
List types = new List();
foreach(Assembly ass in asses)
{
Type t = ass.GetType(className);
if(t != null) types.Add(t);
}
if(types.Count == 1)
return types.First();
else
return null;
}