基于.net框架下的插件系统的实现方法(补充)

    技术2022-05-11  71

    实现方法:

    一、新建一Class Library工程,命名为Test ;

    二、新增一WindowsForm,名称为Form1;

    三、编码成Test.dll文件;

    四、新建一Windows Application工程,名称随便;

    五、在窗口中放一命令按钮,双击命令按钮给按钮增单击事件,事件的代码如下:

            private void button1_Click(object sender, EventArgs e)        {            string lpFileName = "Test.dll";            string Namespace = "Test";            string ClassName = "Form1";

                try            { // 载入程序集                 Assembly MyAssembly = Assembly.LoadFrom(lpFileName);                Type[] type = MyAssembly.GetTypes();                foreach (Type t in type)                {// 查找要调用的命名空间及类                     if (t.Namespace == Namespace && t.Name == ClassName)                    {                        object[] args2 = new object[] { FormWindowState.Maximized };                        object o = Activator.CreateInstance(t);                        o.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, null, o, args2);                        o.GetType().InvokeMember("Show", BindingFlags.InvokeMethod, null, o, null);                    }                }            }            catch (System.NullReferenceException f)            {                MessageBox.Show(f.Message);            }        }

    五、把Test.dll文件拷入第二工程文件所在的路径底下的/bin/Debug底下

    六、编译执行,单击命令按钮即可实现。


    最新回复(0)