Burak Avcı
Burak Avcı

Reputation: 67

C# Add Control To The Form Via Plugin

I'm working on plugin system.I'm trying to add controls via classes/plugins but stackoverflow error all I got

using System;
using System.Windows.Forms;

namespace TGPT_Plugin_Class
{
    public interface MPlugin
    {
        Form CurrentForm { get; set; }
    }
}

Plugin:

    using System;
    using TGPT_Plugin_Class;
    using System.Windows.Forms;

    namespace Plugin_For_TGPT
    {
        public class ControlAdder : MPlugin
        {
        Button b = new Button();
            public ControlAdder ()
        {
        b.Location = new System.Drawing.Point(50, 50);
        b.Text = "Plugin Button";
        b.Size = new System.Drawing.Size(70, 30);
        CurrentForm.Controls.Add(b);
    }



        public Form CurrentForm
        {
            get
            {
                return CurrentForm;
            }
            set
            {
                CurrentForm = value;
            }
        }
    }
}

Main Application:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using TGPT_Plugin_Class;

namespace The_Great_Plugin_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Assembly.LoadFrom(ofd.FileName);
                foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (Type t in a.GetTypes())
                    {
                        if (t.GetInterface("MPlugin") != null)
                        {
                            try
                            {
                                MPlugin pluginclass = Activator.CreateInstance(t) as MPlugin;
                                pluginclass.CurrentForm = this;
                                return;
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
        }
    }
}

Is there anyway to add control remotely? Or Anyway to make better Plugins?

Upvotes: 2

Views: 1765

Answers (1)

Tinwor
Tinwor

Reputation: 7973

There are many possibilites for implenting addin in a c# project:

1) This is an example to illustrate the basic technique:

Plugin architecture using c#

2)in .NET 4 and 4.5 you can use MEF to do much of the plumbing.

3)using .NET 3.5 you have System.AddIn but i think is too complex(you can see an example here if you want)

Upvotes: 2

Related Questions