Hi all,
I'm trying to make a plugin supported windows MDI application but my brain has stopped! I can't build the algorithm!
First of all, my program will read DLLs from plugin directory which is located at the same directory of my MDI parent application, I don't know what I will use for interfaces and classes, for the moment I created an interface which has to set name and I use that name for placing a button on a panel and I created a DLL with that SDK. Now it's working, but I want to add forms to that DLL and when I click on my button, those forms will appear as MDI child in my parent application..
The code that I worked on:
SDKLib.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
namespace drSDK
{
public interface IPlugIn
{
string name();
}
public class Plug
{
public string pName;
public string pPath;
public string pFName;
}
public class Kit
{
public static List<Plug> getAllPlugIns(string path)
{
List<Plug> myPlugs = new List<Plug>();
if (!Directory.Exists(path))
return myPlugs;
string[] dlls = Directory.GetFiles(path, "*.dll");
foreach (string dll in dlls)
{
Assembly asm = Assembly.LoadFile(dll);
Type[] tipler = asm.GetTypes();
foreach (Type tip in tipler)
{
if (tip.GetInterface("IPlugIn") != null)
{
Plug myP = new Plug();
myP.pFName = tip.FullName;
myP.pPath = dll;
object obj = asm.CreateInstance(tip.FullName);
myP.pName = obj.GetType().InvokeMember("name", BindingFlags.InvokeMethod, null, obj, null).ToString();
myPlugs.Add(myP);
}
}
}
return myPlugs;
}
public static object createObject(Plug p)
{
Assembly asm = Assembly.LoadFile(p.pPath);
object obj = asm.CreateInstance(p.pFName);
return obj;
}
}
}
Form1.cs
using drSDK;
.
.
.
List<Plug> myList = null;
private void Form1_Load(object sender, EventArgs e)
{
myList = Kit.getAllPlugIns(Application.StartupPath + "//plugins");
foreach (Plug p in myList)
{
Button b = new Button();
b.Text = p.pName;
b.Click += new EventHandler(b_Click);
NavigatorGroupItemContainerPanel1.Controls.Add(b);
}
void b_Click(object sender, EventArgs e)
{
foreach (Plug p in myList)
{
if (p.pName == (sender as Button).Text)
{
run(p);
}
}
}
void run(Plug p)
{
IPlugIn obj = (IPlugIn)Kit.createObject(p);
.
.
.
.
}
void Pencereler(Form pencere)
{
bool acik = false;
foreach (Form eleman in this.MdiChildren)
{
if (eleman.Text == pencere.Text)
{
acik = true;
eleman.Activate();
}
if (acik == false)
{
pencere.MdiParent = this;
pencere.Show();
}
}
}
To the best of my recollection obj as form was like:
Form form = obj as Form;if (form != null){ form.MdiParent = this; form.Show();}
but I am stuck!
Do I have to get type like...
Type formType = Type.GetType(BLABLA_DLL_BLA_FORM);
Or do I have to Create Instance off the form which DLL includes? Or what kind of way I've to follow for doing this? What interfaces do I have to add for my SDK for DLL? Ok, I give my obj as a form and if form is not
null
set this container, MDI parent of new form and show it... I may add if the form is already opened, Activate it or BringToFront but which form? "...from" which interface? Do I have to add Form to my DLL named by Form? Or any name? (form1...form2...blaForm..) are there any example projects to examine?