I have a WPF application which is supposed to have a plugin system where a string from my plugin is supposed to show up in a MenuItem Header. I have created a plugin api for my plugin system which is not build into the main application but would be used as a reference by both the external plugin and the main application. In the main application, when the plugins are loaded as dll files durring the loading process of the main window, the properties inside my inherited class are null.
I have tried loading assemblies then getting the type an inherited "Plugin" class.
Here is my plugin api library which is used in my main application and the plugin dll.
using System.Windows;
namespace MWPluginAPI
{
public class Plugin
{
public string Description { get; set; }
public string ItemName { get; set; }
public Window PluginWindow { get; set; }
public object Tag { get; set; }
}
}
This is the first plugin library I created for my application.
using MWPluginAPI;
using System.Windows;
namespace ManageDependencies
{
public class ManageDependencies : Plugin
{
public ManageDependencies()
{
ItemName = "Manage Dependencies";
PluginWindow = new ManageDependenciesDialog();
}
}
}
Here is my main application which is using my plugin.
using MWPluginAPI;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Windows;
namespace Minetest_Workshop
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InstallPlugins();
}
private void InstallPlugins()
{
//create list of assemblies
List<Assembly> assemblies = new List<Assembly>();
//array of dll files
string[] assembly_dlls = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"\required\plugins");
//loop through the dlls, load the assemblies, then add them to the list
foreach (string dll in assembly_dlls)
{
Assembly assembly = Assembly.LoadFile(dll);
assemblies.Add(assembly);
}
//loop through assemblies then create a menu item with a Header containing ItemName.
foreach (Assembly assembly in assemblies)
{
Type type = assembly.GetType("ManageDependencies.ManageDependencies");
Plugin plugin = (Plugin)assembly.CreateInstance("ManageDependencies.ManageDependencies");
string ItemName = plugin.ItemName;
MenuItem menuItem = new MenuItem();
menuItem.Header = ItemName;
pluginsMenuItem.Items.Add(null);
}
}
}
}
All of my libraries are WPF User Control Libraries.
When i start my wpf application, there is no string in the menu item that corresponds to the loaded plugins.
I have actually been on the internet for a long time searching for answers but could not find a thing. I'm afraid that this is an impossible task. Thanks for any help. Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire