Skip to content

Instantly share code, notes, and snippets.

@antmrdevlabs
Created May 13, 2016 05:35
Show Gist options
  • Save antmrdevlabs/fec83896f4745c2759692a911487f930 to your computer and use it in GitHub Desktop.
Save antmrdevlabs/fec83896f4745c2759692a911487f930 to your computer and use it in GitHub Desktop.
DynamicAssemblyLoader.cs
public class DynamicAssemblyLoader
{
public Dictionary<string, Assembly> Assemblies = new Dictionary<string, Assembly>();
private void InitalLoadAssemblies()
{
DirectoryInfo dir = new DirectoryInfo(_ExtPath);
foreach (FileInfo f in dir.GetFiles("*ServiceModule.dll"))
{
LoadAssembly(f.FullName);
}
}
private void LoadAssembly(string fullPath)
{
string key = Path.GetFileNameWithoutExtension(fullPath);
Assembly assembly = null;
if (!Assemblies.TryGetValue(key, out assembly))
{
assembly = Assembly.Load(File.ReadAllBytes(fullPath));
Assemblies.Add(key, assembly);
}
}
private void MonitorExtensionDirectory()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Filter = "*ServiceModule.dll";
//watcher.Created += new FileSystemEventHandler(Watcher_ExtensionCreated);
watcher.Changed += new FileSystemEventHandler(Watcher_ExtensionChanged);
watcher.Deleted += new FileSystemEventHandler(Watcher_ExtensionDeleted);
watcher.Path = _ExtPath;
watcher.EnableRaisingEvents = true;
}
private void RemoveAssembly(string assemblyName)
{
Assemblies.Remove(assemblyName);
}
void Watcher_ExtensionChanged(object sender, FileSystemEventArgs e)
{
AppLogger.LogToDatabase(LogPriorities.DEBUG_PRIO_ALL, "*.dll file changed.", e.FullPath);
RemoveAssembly(Path.GetFileNameWithoutExtension(e.FullPath));
LoadAssembly(e.FullPath);
}
void Watcher_ExtensionDeleted(object sender, FileSystemEventArgs e)
{
AppLogger.LogToDatabase(LogPriorities.DEBUG_PRIO_ALL, "*.dll file deleted.", e.FullPath);
RemoveAssembly(Path.GetFileNameWithoutExtension(e.FullPath));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment