Check out the Latest Articles:
My Application Framework – Part Two – Assembly Resolve
In order to use code which exists outside the bin directory we must write a class that loads these assemblies using reflection and finds these assemblies when the system requires them.

The ResolveAssemblies class scans a supplied directory for libraries matching supplied patterns. In my case it scans a Plugins folder for assemblies that begin with ThreeBytes.

1
2
3
4
5
6
7
8
9
10
11
12
13
var scanPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
var directory = new DirectoryInfo(scanPath);
var files = new HashSet<FileInfo>();
try
{
     foreach (var pattern in patterns)
     {
          var found = directory.GetFiles(pattern, SearchOption.AllDirectories);
          files.UnionWith(found);
     }
}
catch (Exception)
{ }

It then attempts to load these assemblies and then register them in a dictionary.

1
2
3
4
5
6
7
8
9
10
11
12
13
foreach (var file in files)
{
     var assembly = Assembly.LoadFrom(file.FullName);
     assembly.GetTypes();
     RegisterAssembly(assembly.FullName, assembly);
}

private void RegisterAssembly(string assemblyName, Assembly assembly)
{
     var comma = assemblyName.IndexOf(',');
     pluginAssembliesByFullName[assemblyName] = assembly;
     pluginAssembliesByFullName[comma == -1 ? assemblyName : assemblyName.Substring(0, comma)] = assembly;
}

Finally our start-up task adds this method when the system needs to find an assembly.

1
2
3
4
private bool TryGetAssembly(string assemblyName, out Assembly assembly)</p>
{
     return PluginAssembliesByFullName.TryGetValue(assemblyName, out assembly);
}

Which simply looks in the dictionary for the assembly the system is looking for.



  1. It‘s quite in here! Why not leave a response?