Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
                //I HAVE TO DO SMTH HERE (IN MY OPINION)
    }
    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>();//list
            if (!Directory.Exists(path))
                return myPlugs;//null
            string[] dlls = Directory.GetFiles(path, "*.dll");//arry dlls
             foreach (string dll in dlls)//all dlls in list
            {
                Assembly asm = Assembly.LoadFile(dll);//open as asm
                Type[] tipler = asm.GetTypes();
                foreach (Type tip in tipler)
                {
                    if (tip.GetInterface("IPlugIn") != null)//Thats my SDK!
                    {
                        Plug myP = new Plug();
                        myP.pFName = tip.FullName;
                        myP.pPath = dll;
                        object obj = asm.CreateInstance(tip.FullName);//dll instance 
                        myP.pName = obj.GetType().InvokeMember("name", BindingFlags.InvokeMethod, null, obj, null).ToString(); //name from interface
                        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;//Plugin list
        private void Form1_Load(object sender, EventArgs e)
        {
            myList = Kit.getAllPlugIns(Application.StartupPath + "//plugins");//plugin directory
            foreach (Plug p in myList)//4 every plug
            {
                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)//wats clicked
                {
                    run(p);
                }
            }
        }
        void run(Plug p)
        {
            IPlugIn obj = (IPlugIn)Kit.createObject(p);
            .
            . //I HAVE TO DO SMTH HERE (IN MY OPINION)
            .
            .

        }
        void Pencereler(Form pencere)//Do not open again and again
        {
            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?
Posted
Updated 26-Aug-10 8:14am
v4

1 solution

Try it first using a child form that is fully known, one for which you don't need your Kit. When you have that working, you can see what methods you should require your IPlugIn object to have, including those inherited from Form. Substituting in the object from CreateInstance should be relatively easy then since you will already have a working code architecture.

BTW, the second if in your Pencereler method belongs outside the foreach, not inside.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900