C# 反射动态加载窗口

之前的文章中,已经详细说明了 反射的用法。这里就不再多说了。

这里就直接上代码

      //点击导航栏项时触发
//所有的导航栏项的点击事件都在此函数中处理
private void navBarItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
EntityMode entityMode
= e.Link.Group.Tag as EntityMode; //模块实体
EntityMenu entityMenu = e.Link.Item.Tag as EntityMenu;//菜单实体

string dllPath = entityMode.Path; //模块路径
string formName = entityMenu.ClassName; //菜单类名

string fullFormName = dllPath + "." + formName + "," + dllPath; //完整的反射路径
Type formType = Type.GetType(fullFormName); //该路径的反射
if (formType != null)//查看反射是否成功
{
if (typeof(Form).IsAssignableFrom(formType)) //反射结果是否为窗体Form 关键的判断
{
CloseOtherAllFrom();
//关闭父窗体中的所有子窗体
Form frm = (Form)Activator.CreateInstance(formType); //创建反射窗体实例 建立窗体的实例
frm.Text = entityMenu.MenuName; //窗体名
frm.MdiParent = this; //设置为子窗体与父窗体关系 作为现在窗体的子窗体
frm.Show();
}
else
{ MessageBox.Show(
"指定的类型不能是从Form类型继承", "温馨提示");}
}
else
{ MessageBox.Show(
"指定的类型不存在", "温馨提示"); }
}

//加载新的子窗体时,关闭其它子窗体
private void CloseOtherAllFrom()
{
foreach (Form frm in this.MdiChildren)
{
frm.Close();
}
}

  

posted @ 2011-09-09 15:44  Tammie-锴  阅读(3458)  评论(0编辑  收藏  举报