Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从磁盘加载WindowsFormsControlLibrary DLL,然后添加到控件,C#_C#_Winforms - Fatal编程技术网

从磁盘加载WindowsFormsControlLibrary DLL,然后添加到控件,C#

从磁盘加载WindowsFormsControlLibrary DLL,然后添加到控件,C#,c#,winforms,C#,Winforms,如何从磁盘加载WindowsFormsControlLibrary用户控件,然后在运行时以编程方式将其添加到Windows窗体中的控件列表 我只想从一个文件名开始 下面的伪代码说明了这一点。明确地说,我不想将其添加到VisualStudio工具箱中,并在设计时使用它。相反,我希望在运行时加载它,并将其插入到Windows窗体中,除了文件名之外,我什么都不知道 网上有很多例子和问题。 一个非常简短的例子: // load your dll System.Reflec

如何从磁盘加载WindowsFormsControlLibrary用户控件,然后在运行时以编程方式将其添加到Windows窗体中的控件列表

我只想从一个文件名开始

下面的伪代码说明了这一点。明确地说,我不想将其添加到VisualStudio工具箱中,并在设计时使用它。相反,我希望在运行时加载它,并将其插入到Windows窗体中,除了文件名之外,我什么都不知道


网上有很多例子和问题。
一个非常简短的例子:

        // 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;
        }