public void OpenEdit(string fileName, bool applyRules = true) { if (applyRules) { ExtensionRule rule = ImportConfig.ExtensionRules.Find(r => r.Extension == Path.GetExtension(fileName)); if (rule != null && GetPlugin(rule.ImporterGuid) is IImportPlugin importer) { OpenEdit(importer, fileName); return; } } List <IImportPlugin> importerList = ImportPlugins.FindAll(p => p.IsImportable(fileName)); if (importerList.Count > 0) { if (importerList.Count == 1) { OpenEdit(importerList[0], fileName); } else if (importerList.Count > 1) { SelectPluginDialog selectPluginDialog = new SelectPluginDialog { Text = string.Format(Properties.Resources.ImportFile, Path.GetFileName(fileName)), PluginList = importerList.ConvertAll <IPlugin>(p => p), }; if (selectPluginDialog.ShowDialog() == DialogResult.OK) { OpenEdit((IImportPlugin)selectPluginDialog.SelectedPlugin, fileName); } } } else { MessageBox.Show(string.Format(Properties.Resources.Msg_ImporterNotFound, fileName), Properties.Resources.Open, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void LoadPlugin(string path) { Assembly assembly = Assembly.LoadFrom(path); try { foreach (Type type in assembly.GetTypes()) { if (type.IsPublic && type.IsClass && !type.IsAbstract && type.GetInterface(typeof(IPlugin).FullName) != null) { IPlugin plugin; try { plugin = (IPlugin)assembly.CreateInstance(type.FullName); } catch (Exception e) { throw new PluginLoadException(string.Format(Properties.Resources.Msg_PluginInstantiationException, type.FullName), e); } if (GetPlugin(plugin.Guid) != null) { plugin.Dispose(); throw new PluginLoadException(string.Format(Properties.Resources.Msg_PluginRegistrationException_DuplicateGuid, type.FullName, plugin.Guid)); } Plugins.Add(plugin); if (plugin is IImportPlugin importPlugin) { ImportPlugins.Add(importPlugin); } if (plugin is IExportPlugin exportPlugin) { ExportPlugins.Add(exportPlugin); } if (plugin is IStartPlugin startPlugin) { StartPlugins.Add(startPlugin); } if (plugin is IHostConncet hc) { try { hc.Initialize(this); } catch (Exception e) { throw new PluginLoadException(string.Format(Properties.Resources.Msg_PluginHostConnectionException, type.FullName), e); } } OnPluginLoaded(new PluginEventArgs(plugin)); } } } catch (PluginLoadException pluginloadException) { string message = pluginloadException.Message + (string.IsNullOrEmpty(pluginloadException.InnerException?.Message) ? "" : Environment.NewLine + Environment.NewLine) + pluginloadException.InnerException?.Message; MessageBox.Show(message, getAssemblyTitle(assembly), MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (ReflectionTypeLoadException reflectionTypeLoadException) { string message = reflectionTypeLoadException.Message + Environment.NewLine + Environment.NewLine + "LoaderExceptions:" + Environment.NewLine; foreach (Exception loaderException in reflectionTypeLoadException.LoaderExceptions) { message += loaderException.Message + Environment.NewLine; } MessageBox.Show(message, getAssemblyTitle(assembly), MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (TargetInvocationException targetInvocationException) { MessageBox.Show(targetInvocationException.InnerException.Message, getAssemblyTitle(assembly), MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception e) { string message = e.Message + (string.IsNullOrEmpty(e.StackTrace) ? "" : Environment.NewLine + Environment.NewLine) + e.StackTrace; MessageBox.Show(message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } string getAssemblyTitle(Assembly _assembly) { object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; if (titleAttribute.Title != "") { return(titleAttribute.Title); } } return(Path.GetFileNameWithoutExtension(_assembly.CodeBase)); } }