从 dll 程序集中动态加载窗体 [原创]

 昨天晚上花了一晚上时间写了一个从程序集中动态加载窗体的程序.将任何包含窗体的代码编译成 dll 文件,再把 dll 文件拷贝到本程序的目录下,本程序运行时即可动态检查到 dll 文件中的窗体,将窗体类的类型在程序菜单中显示出来,点击菜单即可运行对应的窗体.

  本程序主要用到了 Assembly 类动态加载程序集,再得到程序集中包含类的 Type 类型,动态生成类实例,动态调用类方法.个人觉得这是一种提供高度松耦合,可随意扩展的程序结构框架,希望和大家探讨一下这种框架的应用前景!

  关键性代码如下:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.IO;
None.gif
using  System.Reflection;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
namespace  WindowsFormTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FrmMain : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int formNum;
InBlock.gif        
private ArrayList formTypes = new ArrayList();
InBlock.gif        
private ArrayList formObjects = new ArrayList();
InBlock.gif
InBlock.gif        
private System.Windows.Forms.MainMenu mnuMain;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmRun;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItemWindow;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmHelp;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmCascade;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmTileHorizontal;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmTileVertical;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItem1;
InBlock.gif        
private System.Windows.Forms.MenuItem mnuItmAbout;
InBlock.gif        
private System.Windows.Forms.StatusBar staBarMain;
InBlock.gif        
private System.Windows.Forms.StatusBarPanel pnlInfo;
InBlock.gif        
private System.Windows.Forms.StatusBarPanel pnlNum;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public FrmMain()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.mnuMain = new System.Windows.Forms.MainMenu();
InBlock.gif            
this.mnuItmRun = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItemWindow = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItmCascade = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItmTileHorizontal = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItmTileVertical = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItem1 = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItmHelp = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.mnuItmAbout = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.staBarMain = new System.Windows.Forms.StatusBar();
InBlock.gif            
this.pnlInfo = new System.Windows.Forms.StatusBarPanel();
InBlock.gif            
this.pnlNum = new System.Windows.Forms.StatusBarPanel();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pnlInfo)).BeginInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pnlNum)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// mnuMain
InBlock.gif            
// 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                    
this.mnuItmRun,
InBlock.gif                                                                                    
this.mnuItemWindow,
ExpandedSubBlockEnd.gif                                                                                    
this.mnuItmHelp}
);
InBlock.gif            
// 
InBlock.gif            
// mnuItmRun
InBlock.gif            
// 
InBlock.gif
            this.mnuItmRun.Index = 0;
InBlock.gif            
this.mnuItmRun.Text = "运行窗体(&R)";
InBlock.gif            
// 
InBlock.gif            
// mnuItemWindow
InBlock.gif            
// 
InBlock.gif
            this.mnuItemWindow.Index = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.mnuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                          
this.mnuItmCascade,
InBlock.gif                                                                                          
this.mnuItmTileHorizontal,
InBlock.gif                                                                                          
this.mnuItmTileVertical,
ExpandedSubBlockEnd.gif                                                                                          
this.menuItem1}
);
InBlock.gif            
this.mnuItemWindow.Text = "窗口(&W)";
InBlock.gif            
// 
InBlock.gif            
// mnuItmCascade
InBlock.gif            
// 
InBlock.gif
            this.mnuItmCascade.Index = 0;
InBlock.gif            
this.mnuItmCascade.Text = "层叠窗口";
InBlock.gif            
this.mnuItmCascade.Click += new System.EventHandler(this.mnuItmCascade_Click);
InBlock.gif            
// 
InBlock.gif            
// mnuItmTileHorizontal
InBlock.gif            
// 
InBlock.gif
            this.mnuItmTileHorizontal.Index = 1;
InBlock.gif            
this.mnuItmTileHorizontal.Text = "横向平铺窗口";
InBlock.gif            
this.mnuItmTileHorizontal.Click += new System.EventHandler(this.mnuItmTileHorizontal_Click);
InBlock.gif            
// 
InBlock.gif            
// mnuItmTileVertical
InBlock.gif            
// 
InBlock.gif
            this.mnuItmTileVertical.Index = 2;
InBlock.gif            
this.mnuItmTileVertical.Text = "纵向平铺窗口";
InBlock.gif            
this.mnuItmTileVertical.Click += new System.EventHandler(this.mnuItmTileVertical_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItem1
InBlock.gif            
// 
InBlock.gif
            this.menuItem1.Index = 3;
InBlock.gif            
this.menuItem1.Text = "-";
InBlock.gif            
// 
InBlock.gif            
// mnuItmHelp
InBlock.gif            
// 
InBlock.gif
            this.mnuItmHelp.Index = 2;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.mnuItmHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
ExpandedSubBlockEnd.gif                                                                                       
this.mnuItmAbout}
);
InBlock.gif            
this.mnuItmHelp.Text = "帮助(&H)";
InBlock.gif            
// 
InBlock.gif            
// mnuItmAbout
InBlock.gif            
// 
InBlock.gif
            this.mnuItmAbout.Index = 0;
InBlock.gif            
this.mnuItmAbout.Shortcut = System.Windows.Forms.Shortcut.F1;
InBlock.gif            
this.mnuItmAbout.Text = "关于(&A)";
InBlock.gif            
this.mnuItmAbout.Click += new System.EventHandler(this.mnuItmAbout_Click);
InBlock.gif            
// 
InBlock.gif            
// staBarMain
InBlock.gif            
// 
InBlock.gif
            this.staBarMain.Location = new System.Drawing.Point(0343);
InBlock.gif            
this.staBarMain.Name = "staBarMain";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.staBarMain.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] dot.gif{
InBlock.gif                                                                                          
this.pnlInfo,
ExpandedSubBlockEnd.gif                                                                                          
this.pnlNum}
);
InBlock.gif            
this.staBarMain.ShowPanels = true;
InBlock.gif            
this.staBarMain.Size = new System.Drawing.Size(58422);
InBlock.gif            
this.staBarMain.TabIndex = 1;
InBlock.gif            
// 
InBlock.gif            
// pnlInfo
InBlock.gif            
// 
InBlock.gif
            this.pnlInfo.Width = 300;
InBlock.gif            
// 
InBlock.gif            
// pnlNum
InBlock.gif            
// 
InBlock.gif
            this.pnlNum.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
InBlock.gif            
this.pnlNum.Width = 268;
InBlock.gif            
// 
InBlock.gif            
// FrmMain
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(584365);
InBlock.gif            
this.Controls.Add(this.staBarMain);
InBlock.gif            
this.IsMdiContainer = true;
InBlock.gif            
this.Menu = this.mnuMain;
InBlock.gif            
this.Name = "FrmMain";
InBlock.gif            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
InBlock.gif            
this.Text = "动态加载窗体";
InBlock.gif            
this.Load += new System.EventHandler(this.FrmMain_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pnlInfo)).EndInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pnlNum)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void FrmMain_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Assembly assembly 
= null;
InBlock.gif            
string windowsPath = Path.Combine(Application.StartupPath, "Windows");
InBlock.gif
InBlock.gif            
foreach (string dllFile in Directory.GetFiles(windowsPath, "*.dll"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                assembly 
= Assembly.LoadFile(dllFile);
InBlock.gif                Type[] types 
= assembly.GetTypes();
InBlock.gif                
InBlock.gif                
foreach (Type t in types)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (t.BaseType == typeof(Form))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.formTypes.Add(t);
InBlock.gif                        MenuItem item 
= this.mnuItmRun.MenuItems.Add(t.FullName);
InBlock.gif                        item.Click 
+= new EventHandler(menuItemNewItem_Click);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemNewItem_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MenuItem item 
= (MenuItem)sender;
InBlock.gif
InBlock.gif            Type t 
= (Type)(this.formTypes[item.Index]);
InBlock.gif
InBlock.gif            Object obj 
= Activator.CreateInstance(t);
InBlock.gif            
this.formObjects.Add(obj);
InBlock.gif            formNum 
+= 1;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            t.InvokeMember(
"MdiParent", BindingFlags.SetProperty, null, obj, new object[] dot.gif{this});
ExpandedSubBlockStart.gifContractedSubBlock.gif            t.InvokeMember(
"Text", BindingFlags.SetProperty, null, obj, new object[] dot.gif{t.FullName+"  窗体:"+formNum});
ExpandedSubBlockStart.gifContractedSubBlock.gif            t.InvokeMember (
"Show", BindingFlags.InvokeMethod, null, obj, new object [] dot.gif{});
InBlock.gif
InBlock.gif            ((Form)obj).Closing 
+= new CancelEventHandler(FrmWindow_Closing);
InBlock.gif            ((Form)obj).Activated 
+= new EventHandler(FrmWindow_Activated);
InBlock.gif            MenuItem menuItem 
= this.mnuItemWindow.MenuItems.Add(((Form)obj).Text);
InBlock.gif            menuItem.Click 
+= new EventHandler(menuItemWindow_Click);
InBlock.gif
InBlock.gif            
this.pnlNum.Text = "当前装载了"+this.formObjects.Count+"个窗体";
InBlock.gif            
this.pnlInfo.Text = "当前活动窗体:"+this.ActiveMdiChild.Text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemWindow_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MenuItem item 
= (MenuItem)sender;
InBlock.gif
InBlock.gif            ((Form)(
this.formObjects[item.Index-4])).Activate();
InBlock.gif
InBlock.gif            
this.pnlNum.Text = "当前装载了"+this.formObjects.Count+"个窗体";
InBlock.gif            
this.pnlInfo.Text = "当前活动窗体:"+this.ActiveMdiChild.Text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FrmWindow_Activated(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.pnlNum.Text = "当前装载了"+this.formObjects.Count+"个窗体";
InBlock.gif            
this.pnlInfo.Text = "当前活动窗体:"+this.ActiveMdiChild.Text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FrmWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i=0; i<this.formObjects.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (((Form)this.formObjects[i]).Text == ((Form)sender).Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.formObjects.RemoveAt(i);
InBlock.gif                    
this.mnuItemWindow.MenuItems.RemoveAt(i+4);
InBlock.gif
InBlock.gif                    
this.pnlNum.Text = "当前装载了"+this.formObjects.Count+"个窗体";
InBlock.gif                    
this.pnlInfo.Text = "当前活动窗体:"+this.ActiveMdiChild.Text;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void mnuItmCascade_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.LayoutMdi(MdiLayout.Cascade);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void mnuItmTileHorizontal_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.LayoutMdi(MdiLayout.TileHorizontal);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void mnuItmTileVertical_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.LayoutMdi(MdiLayout.TileVertical);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void mnuItmAbout_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
new FrmAbout().ShowDialog(this);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

程序源码下载:/Files/Infinity/WindowsForm.rar
  
  程序截图:

单个 dll 文件包含一个窗体时:
003.JPG
当程序目录下的所有程序集中包含一个窗体类时,程序运行界面:
004.JPG

多个 dll 文件包含多个窗体时:
002.JPG
当程序目录下的所有程序集中包含多个窗体类时,程序运行界面:
001.JPG


  欢迎大家提出意见,共同探讨!

转载于:https://www.cnblogs.com/Infinity/archive/2006/10/26/540347.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值