//#region Lien quan den plugin private String[] InstallPluginDLLs(PluginTrustLevel trustLevel) { // File dialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Plugin Files (*.dll)|*.dll"; dialog.FilterIndex = 1; dialog.RestoreDirectory = true; String copyToPath; if (trustLevel == PluginTrustLevel.Full) { copyToPath = PluginManager.FullTrustPath; dialog.Title = "Select Plugin DLL to Install Fully-Trusted"; } else { copyToPath = PluginManager.SemiTrustPath; dialog.Title = "Select Plugin DLL to Install Semi-Trusted"; } dialog.Multiselect = true; ArrayList list = new ArrayList(); DialogResult ret = STAShowDialog(dialog); if (ret == DialogResult.OK) { foreach (String dll in dialog.FileNames) { String name = Path.GetFileName(dll); if (!PluginAlreadyInstalled(name)) { try { File.Copy(dll, Path.Combine(copyToPath, name)); list.Add(name); } catch (IOException) { MessageBox.Show("Unable to install dll: " + dll, "Error"); } } else { MessageBox.Show("Plugin dll already installed. To uninstall," + " delete the .dll file from the Plugins directory.", "Plugin Already Installed"); } } } return (String[])list.ToArray(typeof(String)); }
void LoadPluginsHelper(PluginTrustLevel level) { Type[] types; types = PluginManager.LoadPluginTypes( level, PluginCriteria.Interface, typeof(IPageGroupProvider)); InstantiateUIElements(types, instantiatedPageGroupPluginTypes, new UpdateUICallback(UpdatePageGroup), true); types = PluginManager.LoadPluginTypes( level, PluginCriteria.Interface, typeof(IBarButtonProvider)); InstantiateUIElements(types, instantiatedBarButtonPluginTypes, new UpdateUICallback(UpdateBarButton), true); //types = PluginManager.LoadPluginTypes( // level, PluginCriteria.BaseClass, // typeof(DocumentForm)); //InstantiateUIElements(types, instantiatedDocumentPluginTypes, // new UpdateUICallback(UpdateDocument), false); }
// Get an array of plugin types using a // plugin criteria and trust level public static Type[] LoadPluginTypes( PluginTrustLevel level, PluginCriteria criteria, Type criteriaType) { // Build the path for plugins we are searching for // also, if secure plugins won't work, throw String pluginPath; if(level == PluginTrustLevel.Full){ pluginPath = fullTrustPath; }else{ if(!supportSecurePlugins){ throw new SecurityException( "Semi-trusted plugins were not enabled!"); } pluginPath = semiTrustPath; } // Create a temporary app domain so that we can unload assemblies AppDomain domain = AppDomain.CreateDomain("TempDom"+Guid.NewGuid().ToString()); // Find the name of this assembly and load it into the domain String assemblyName = Assembly.GetExecutingAssembly().FullName; domain.Load(assemblyName); // Create an instance of the PluginManager type // in the temporary domain BindingFlags binding = BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Instance; ObjectHandle handle = domain.CreateInstance( assemblyName, typeof(PluginManager).ToString(), false, binding, null, null, null, null, null); PluginManager helper = (PluginManager)handle.Unwrap(); try{ // Get an array of assemblies that include matching plugins String[] assemblies= helper.DiscoverPluginAssembliesHelper( pluginPath, criteria, criteriaType); // return loaded plugin types return LoadPluginTypesHelper(assemblies, criteria, criteriaType); }catch(ReflectionTypeLoadException){ return new Type[0]; }finally{ // Unload any unwanted assemblies AppDomain.Unload(domain); } }