从磁盘加载WindowsFormsControlLibrary DLL,然后添加到控件,C#
如何从磁盘加载WindowsFormsControlLibrary用户控件,然后在运行时以编程方式将其添加到Windows窗体中的控件列表 我只想从一个文件名开始 下面的伪代码说明了这一点。明确地说,我不想将其添加到VisualStudio工具箱中,并在设计时使用它。相反,我希望在运行时加载它,并将其插入到Windows窗体中,除了文件名之外,我什么都不知道从磁盘加载WindowsFormsControlLibrary DLL,然后添加到控件,C#,c#,winforms,C#,Winforms,如何从磁盘加载WindowsFormsControlLibrary用户控件,然后在运行时以编程方式将其添加到Windows窗体中的控件列表 我只想从一个文件名开始 下面的伪代码说明了这一点。明确地说,我不想将其添加到VisualStudio工具箱中,并在设计时使用它。相反,我希望在运行时加载它,并将其插入到Windows窗体中,除了文件名之外,我什么都不知道 网上有很多例子和问题。 一个非常简短的例子: // load your dll System.Reflec
网上有很多例子和问题。
一个非常简短的例子:
// load your dll
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("SomeUserControl.dll");
// to get the right type use namespace.controlname
Type type = assembly.GetType("MyControlLibrary.MyUserCtl");
// create an instance
object instanceOfMyType = Activator.CreateInstance(type);
// do something with Control
Control ctrl = instanceOfMyType as Control;
if (ctrl != null)
{
this.Controls.Add(ctrl);
ctrl.Dock = DockStyle.Bottom;
}
不要忘了将此包装在try/catch中,这里也可以是和的可能副本:
// load your dll
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("SomeUserControl.dll");
// to get the right type use namespace.controlname
Type type = assembly.GetType("MyControlLibrary.MyUserCtl");
// create an instance
object instanceOfMyType = Activator.CreateInstance(type);
// do something with Control
Control ctrl = instanceOfMyType as Control;
if (ctrl != null)
{
this.Controls.Add(ctrl);
ctrl.Dock = DockStyle.Bottom;
}