private void (object obj)
{
switch(obj.GetType().Name)
case "WinFrom1";
.....
break;
case "WinForm2":
......
break;
}
{
switch(obj.GetType().Name)
case "WinFrom1";
.....
break;
case "WinForm2":
......
break;
}
Form1 f = new Form1();调用函数如下:
xxx.xx(f);
就ok了
然后xx里面
根据f.GetType().Name进行switch
就要用反射
关键是你要进行什么操作
如果你能保证,任何窗体里面都有一个名字相同的方法
而你要处理的就是调用这个方法
那么通过反射来作你能确定吗?
customer.customer_manager cust=null;
foreach(Form f in this.MdiChildren)
{
if(f is customer.customer_manager)
{
cust= (customer.customer_manager)f;
break;
}
}
if( cust!= null)
{
cust.ShowDialog();
cust.Focus();
}
else
{
cust=new customer.customer_manager();
// cust.MdiParent=this;
cust.ShowDialog();
cust.Focus();
}
这样的,我就需要在每一个打开窗体的事件中都需要重写这段代码,只是改一个窗体名字,我窗体很多,挨个使用switch也挺麻烦
{
object obj = null;
foreach(Form f in this.MdiChildren)
{
if(f is t)
{
obj = f; break;
}
}
if(obj ==null)
{
obj = System.Activator.CreateInstance(t); }
MethodInfo mif = t.GetMethod("Focus");
mif.Invoke(obj,null);
}
public void ShowForms(Type t)
--->
public void ShowForms(string yourFormClassName)//传递你的窗体的类的名称,加上命名空间!
{
Type t = Type.GetType(yourFormClassName);
.....下面的不变
}