日期:2014-05-18 浏览次数:20982 次
Form1 a1=new Form1();
.....
Formn an=new Formn();
private void button_Click(object sender, EventArgs e)
{
if(a1==null||a1.Isdisposed)
{
a1=new Form1();
}
}
//单例窗体,适当改动代码
protected void ShowMdiChild<TForm>() where TForm : Form, new()
{
Form child = this.MdiChildren.FirstOrDefault(frm => frm is TForm);
if (child == null)
{
child = new TForm();
child.MdiParent = this;
child.StartPosition = FormStartPosition.WindowsDefaultLocation;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
else
{
child.StartPosition = FormStartPosition.WindowsDefaultLocation;
child.WindowState = FormWindowState.Maximized;
child.Select();
}
}
//调用方法
private void button_Click(object sender, EventArgs e)
{
this.ShowMdiChild<a1>();
}
------解决方案--------------------
static Hashtable arr = new Hashtable();
public static T GetInstance<T>(string className){
if (arr[className] == null){
arr[className] = (T)System.Reflection.Assembly.Load("命名空间").CreateInstance("命名空间."+className);
}
return arr[className] as T;
}
------解决方案--------------------
static Hashtable arr = new Hashtable();
public static T GetInstance<T>() where T : class {
var t = typeof(T);
var k = t.FullName;
if (arr[k] == null){
arr[k] = (T)t.Assembly.CreateInstance(t.FullName);
}
return arr[k] as T;
}