为更好的代码重用,公司做了一套WinForm的框架,集成数据操作\用户权限等功能,并进行相应的封装.
基于此框架可以任意新建应用,然后通过数据库配置,使用框架调用新开发程序集中的类或窗体就可以了,基于这种想法,采用反射的实现方式.
根据构造函数不同,可以使用默认的构造函数,或带参的构造函数(可以在数据库中配好参数类型等,具体值在调用时传入). 一小段代码
1 private void CreatUnits(string pstrBuildingID)
2 {
3 string strAssembly = "Business.UI.dll";
4 string frmName = "Business.UI.frmUnits";
5 foreach (Form frmTemp in this.MdiChildren)
6 if (frmTemp.GetType().ToString() == frmName.Trim())
7 {
8 if (frmTemp.WindowState == FormWindowState.Minimized)
9 frmTemp.WindowState = FormWindowState.Normal;
10 frmTemp.Close();
11 }
12
13 Assembly assembly = Assembly.LoadFrom(Application.StartupPath + "\\" + strAssembly);
14 Type type = assembly.GetType(frmName);
15 object[] args = { pstrBuildingID };
16 Form frmChild = (Form)Activator.CreateInstance(type,args);
17 frmChild.MdiParent = this;
18 frmChild.WindowState = FormWindowState.Normal;
19 frmChild.Show();
20 frmChild.Dock = System.Windows.Forms.DockStyle.Fill;//平铺,最大化
21 }
2 {
3 string strAssembly = "Business.UI.dll";
4 string frmName = "Business.UI.frmUnits";
5 foreach (Form frmTemp in this.MdiChildren)
6 if (frmTemp.GetType().ToString() == frmName.Trim())
7 {
8 if (frmTemp.WindowState == FormWindowState.Minimized)
9 frmTemp.WindowState = FormWindowState.Normal;
10 frmTemp.Close();
11 }
12
13 Assembly assembly = Assembly.LoadFrom(Application.StartupPath + "\\" + strAssembly);
14 Type type = assembly.GetType(frmName);
15 object[] args = { pstrBuildingID };
16 Form frmChild = (Form)Activator.CreateInstance(type,args);
17 frmChild.MdiParent = this;
18 frmChild.WindowState = FormWindowState.Normal;
19 frmChild.Show();
20 frmChild.Dock = System.Windows.Forms.DockStyle.Fill;//平铺,最大化
21 }
注意一下 Application.StartupPath 这个最好加上,否则一但程序打开\保存文件后,再加载Assembly的路径就不对了,容易出问题.