using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Chernobyl.Collections.Generic.Event; using Chernobyl.Creation; namespace Chernobyl.Plugin { /// /// Creates plug-ins from assembly files. /// public class AssemblyFilePluginFactory : PluginBuilder, IFactory, Object[]> { /// /// Constructor. /// /// The instance that /// gives and takes services and is given to the created plug-ins /// if they have a constructor that takes an /// instance. public AssemblyFilePluginFactory(IEventCollection services) : this(services, assembly => { /* By default, do nothing with assemblies. */ }) { } /// /// Constructor. /// /// The instance that /// gives and takes services and is given to the created plug-ins /// if they have a constructor that takes an /// instance. /// An optional, additional action performed /// on loaded assemblies prior to the plug-ins being created. public AssemblyFilePluginFactory(IEventCollection services, Action processAssembly) : base(services) { processAssembly.ThrowIfNull("processAssembly"); _processAssembly = processAssembly; } /// public object[] Create(IEnumerable assemblyNames) { try { List interfaces = new List(); foreach (AssemblyName name in assemblyNames) { Assembly moduleAssembly = Assembly.Load(name); _processAssembly(moduleAssembly); // go through all of the types in the module, find the types // specified by the predicate and create it. Type[] types = moduleAssembly.GetTypes(); interfaces.AddRange(from type in types where Predicate(type) select Creator(type)); } var result = interfaces.ToArray(); ReportCreation(result); return result; } catch (Exception e) { ReportCreation(new CreationEventArgs(e)); throw; } } /// /// An optional, additional action performed on loaded assemblies prior /// to the plug-ins being created. /// readonly Action _processAssembly; } }