private static Dictionary<SledDynamicPluginInfo, Assembly> GetDynamicAssemblies(string directory, Type[] attributeTypes) { var assemblies = new Dictionary<SledDynamicPluginInfo, Assembly>(); try { var files = Directory.GetFiles(directory, "*.dll", SearchOption.TopDirectoryOnly); foreach (var file in files) { try { Assembly assembly; try { assembly = Assembly.LoadFile(file); } catch (Exception) { continue; } if (assembly == null) continue; // Check if assembly has any of the // attributes we're looking for var bAssemblyContainsAnyAttributes = DoesAssemblyContainAnyAttributes( assembly, attributeTypes); // No attributes; skip if (!bAssemblyContainsAnyAttributes) continue; // Check if plugin can be instantiated Exception exception; var bCanPluginBeInstantiated = CanAssemblyBeInstantiated( assembly.GetExportedTypes(), out exception); // Create plugin info var pluginInfo = new SledDynamicPluginInfo( Path.GetFileName(file), file, bCanPluginBeInstantiated, exception); // Add assemblies.Add(pluginInfo, assembly); } catch (Exception ex) { SledOutDevice.OutLine( SledMessageType.Error, "Exception loading plugin \"{0}\": {1}", file, ex.Message); } } } catch (Exception ex) { SledOutDevice.OutLine( SledMessageType.Error, "Exception looking for plugins: {0}", ex.Message); } return assemblies; }
private static string GetString(SledDynamicPluginInfo info) { var name = Path.GetFileName(info.AbsolutePath); if (info.Loaded) { return string.Format( "Plugin finder found {0} " + "and it loaded successfully!", name); } var exMessage = info.Exception == null ? string.Empty : string.Format( "Exception: {0}", info.Exception.Message); return string.Format( "Plugin finder found {0} " + "but it failed to load! {1}", name, exMessage); }