动态调用用dll封装的控件

来源:互联网 发布:根据小说改编的网络剧 编辑:程序博客网 时间:2024/04/30 06:26

今天碰到一个问题,一个人说什么dll容器其实就是winform编译后的exe文件,意思就是通过这个exe去动态调用已经编译好的用户控件,也就是dll文件。这里说的动态顾名思义,就是不能在程序里面写死调用哪个dll,而是像我程序截图一样 是通过filediologue选择而定的。这样就用到了.NET非常牛叉的技术,反射了。。。

点击文本框后弹出文件选择对话框:

       private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;
            this.textBox1.Text = file;
            string className =openFileDialog1.SafeFileName.Split('.')[0]+ ".UserControl1";
            this.textBox2.Text = className;
            //Object uc = Assembly.LoadFrom(file).CreateInstance(className);//无参够着函数
            //object[] objs=new object[]{"FFFFF"};
            Object uc = Assembly.LoadFrom(file).CreateInstance(className, false, BindingFlags.CreateInstance, null,new String[]{"5555"}, null, null);//调用有参数的构造函数
            UserControl uu =(UserControl)uc;
            
            this.panel1.Controls.Add(uu);
  }



原创粉丝点击