C# Activator的用法
生活随笔
收集整理的這篇文章主要介紹了
C# Activator的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要動態的創建一個實例模型的時候,就用Activator.CreateInstance(Type type);如果是明確的知道要創建哪個實例的模型,就可以用 new
C#在類工廠中動態創建類的實例,所使用的方法為:
1. Activator.CreateInstance (Type)
2. Activator.CreateInstance (Type, Object[])
兩種方法區別僅為:創建無參數的構造方法和創建有參數的構造函數。
private Form LoadMdiChild(Type formclass)
{
Form nForm = null;
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == formclass)
{
nForm = frm;
break;
}
}
if (nForm == null)
{
nForm = (Form)(Activator.CreateInstance(formclass,this));
//nForm.Owner = this;
nForm.MdiParent = this;
nForm.Left = 0;
nForm.Top = 0;
nForm.Dock = System.Windows.Forms.DockStyle.Fill;
nForm.FormBorderStyle = FormBorderStyle.None;
nForm.MaximizeBox = false;
nForm.MinimizeBox = false;
nForm.ControlBox = false;
}
return nForm;
}
總結
以上是生活随笔為你收集整理的C# Activator的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电信路由器怎么连接电信路由器如何联网
- 下一篇: 华为路由器亮红灯华为路由器红灯如何变成黄