C#通过插件向表单添加控件
我正在开发插件系统。我试图通过类/插件添加控件,但我得到的都是stackoverflow错误C#通过插件向表单添加控件,c#,C#,我正在开发插件系统。我试图通过类/插件添加控件,但我得到的都是stackoverflow错误 using System; using System.Windows.Forms; namespace TGPT_Plugin_Class { public interface MPlugin { Form CurrentForm { get; set; } } } 插件: using System; using TGPT_Plugin_Cla
using System;
using System.Windows.Forms;
namespace TGPT_Plugin_Class
{
public interface MPlugin
{
Form CurrentForm { get; set; }
}
}
插件:
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;
}
}
}
}
主要用途:
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
{
}
}
}
}
}
}
}
}
是否有远程添加控件的方法?或者无论如何,为了制作更好的插件?在c#项目中实现插件有很多可能性: 1) 这是一个说明基本技术的示例: 2) 在.NET4和4.5中,您可以使用它来完成大部分管道工作
3) 使用.NET 3.5,您有System.AddIn,但我认为太复杂了(如果您愿意,您可以查看一个示例)MEF不是一个guy构建的类吗?“托管扩展性框架(MEF)是一个用于.NET的合成层,可提高大型应用程序的灵活性、可维护性和可测试性。MEF可用于第三方插件扩展,也可将松散耦合的插件式体系结构的好处带给常规应用程序。”摘自MEF主页+1。我认为这里的重点是不要重新发明轮子。NET具有插件的内置功能,所以请使用它。