private static void Test_Mef() { var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(asmCatalog, true); var mi1 = new MultiInstanceComposer <IA>(container); mi1.Refresh(); }
private void ReloadModulesInner() { const string LOG_TAG = "ReloadModules"; this.DisposeOldModules(); var container = new CompositionContainer(this.ModuleCompositionCatalog, isThreadSafe: false); var composer = new MultiInstanceComposer <IModule>(container); composer.RefreshIfNeeded(); var ex = composer.Instances .ForAllAsync((m, state) => { if (m.IsDisposed) { return; } if (!m.IsInitialized) { var ctx = new SimpleModuleInitializeContext() { }; m.Initialize(ctx); } }, actionState: new { Shell = this, }, throwExceptions: false); if (ex != null) { this.Logger .Log(msg: ex, categories: LoggerFacadeCategories.Errors, tag: LOG_TAG); } this.Modules = composer.Instances .Where(m => m.IsInitialized && !m.IsDisposed) .ToArray(); }
// Public Methods (2) /// <summary> /// Reloads the list of plugins. /// </summary> public void ReloadPlugIns() { try { var loadedPlugIns = new List <IPlugIn>(); var plugInDir = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "PlugIns")); if (plugInDir.Exists) { foreach (var file in plugInDir.GetFiles("*.dll")) { try { var asmBlob = File.ReadAllBytes(file.FullName); var asm = Assembly.Load(asmBlob); var catalog = new AssemblyCatalog(asm); var ctx = new PlugInContext(); ctx.Assembly = asm; ctx.AssemblyFile = file.FullName; var container = new CompositionContainer(catalog, isThreadSafe: true); container.ComposeExportedValue <global::MarcelJoachimKloubert.DragNBatch.PlugIns.IPlugInContext>(ctx); var instances = new MultiInstanceComposer <IPlugIn>(container); instances.RefeshIfNeeded(); // service locator { var mefLocator = new ExportProviderServiceLocator(container); var sl = new DelegateServiceLocator(mefLocator); ctx.ServiceLocator = sl; } var initializedPlugIns = new List <IPlugIn>(); foreach (var i in instances.Instances) { try { i.Initialize(ctx); initializedPlugIns.Add(i); } catch (Exception ex) { this.OnError(ex); } } ctx.PlugIns = initializedPlugIns.ToArray(); loadedPlugIns.AddRange(initializedPlugIns); } catch (Exception ex) { this.OnError(ex); } } } this.PlugIns.Clear(); this.PlugIns.AddRange(loadedPlugIns); } catch (Exception ex) { this.OnError(ex); } }