Hi!
Folgender Code stammt aus einem Projekt von mir, welches schon eine ganze Weile zurückliegt. Er kann also noch Jugendsünden aus einr Zeit enthalten, wo ich in .NET noch nicht so firm war
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78:
| class PluginCollection { private List<Plugin> plugins; private int guiPluginCount;
public PluginCollection(IHostForm Hostform, ListBox lb_plugins) { string dirName = new FileInfo(Application.ExecutablePath).DirectoryName; string[] files = Directory.GetFiles(dirName, "*.plugin.dll");
plugins = new List<Plugin>(files.Length); foreach (string aFile in files) LoadPlugin(aFile, Hostform, lb_plugins); }
public int GUIPluginCount { get { return guiPluginCount; } }
public int PluginCount { get { return plugins.Count; } }
private void LoadPlugin(string pluginName, IHostForm hostForm, ListBox lb_plugins) { if (File.Exists(pluginName)) { Assembly plugAsm = Assembly.LoadFrom(pluginName);
string pluginTitle = Path.GetFileName(pluginName); pluginTitle = pluginTitle.Substring(0, pluginTitle.IndexOf(".plugin.dll")); Type guiClass = plugAsm.GetType("SharpPix.Plugins." + pluginTitle + ".PluginGUI"); Type codeClass = plugAsm.GetType("SharpPix.Plugins." + pluginTitle + ".PluginCode");
if (codeClass != null) { IGuestCode code = (IGuestCode)Activator.CreateInstance(codeClass); code.HostForm = hostForm;
UserControl gui = null; if (guiClass != null && guiClass.BaseType == typeof(UserControl)) { gui = (UserControl)Activator.CreateInstance(guiClass); ((IGuestGUI)gui).HostForm = hostForm; guiPluginCount++; } Plugin p = new Plugin((IGuestGUI)gui, code);
if (p.Gui != null) lb_plugins.Items.Add(p);
plugins.Add(p); } } }
public IEnumerator<Plugin> GetEnumerator() { foreach (Plugin p in plugins) { yield return p; } }
} |
Zur Erklärung:
Die Klasse soll alle für die Anwendung gefundenen Plugins halten.
Ein Plugin besteht mindestens aus Code, kann aber auch eine GUI enthalten, muss den Namen "{PLUGIN}.plugin.dll" enden und im Ordner der Hauptanwendung liegen (erste Sünde

).
Die Klasse für Code muss dann im Namespace "SharpPix.Plugins.{PLUGIN}" liegen (SharpPix war der Programmname), und den Namen "PluginCode" haben, die Klasse für GUI im selben Namespace liegen und entsprechend "PluginGUI" heißen. Der Code muss IGuestCode implementieren und die GUI muss von UserControl abgeleitet sein und IGuestGUI implementieren.
Die Hostform im Programm implementiert IHostForm, welches ganz viele Events bereitstellt, in die sich ein Plugin einklinken kann.
Die Plugins werden in der Liste in Form dieser Klasse gehalten:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
| public class Plugin { private IGuestGUI gui;
public IGuestGUI Gui { get { return gui; } set { gui = value; } }
private IGuestCode code; public IGuestCode Code { get { return code; } set { code = value; } }
public Plugin(IGuestGUI Gui, IGuestCode Code) { gui = Gui; code = Code;
Gui.Plugin = this; Code.Plugin = this; }
public override string ToString() { return gui.Caption; } } |
Ich hoffe, dass ich Dir damit ein paar Anregungen gegeben habe, wie man so etwas umsetzen kann
Ach ja, damals gab es den System.AddIns-Namespace noch nicht
Grüße
Christian
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".