我的单件模式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;

namespace 反射
{

    #region MyClass
    public class MyClass
    {
       public  string name = "names";
        public MyClass()
        {
            MessageBox.Show("ok");
        }
    }
    #endregion
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Hashtable HashtableForm;          // 哈希 table  全局变量 

        private void button1_Click(object sender, EventArgs e)
        {
            CreateFormHT("Form2");       //字符串类名     实现的单件模式

            MyClass m1 = CreateFormHTClass<MyClass>();
            MessageBox.Show(m1.name);
        }

        void Formtemplet_FormClosed(object sender, FormClosedEventArgs e)
        {
            HashtableForm.Remove(sender.GetType());//   给Form 模板 增加的关闭事件,关闭则在Hashtable中去除记录
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HashtableForm = new Hashtable();
        }

        #region 用于MDI
        void CreateFormHT<T>()
        {
           // Assembly assembly = Assembly.Load("反射");                   //映射此名称的名空间
            Type type = typeof(T);                                       //查看泛型 T 的数据类型
            Form Formtemplet = (Form)Activator.CreateInstance(type);    //根据 T 类型 创建 窗体
            {
                if (!HashtableForm.Contains(type))                      //如果 Hashtable中没有包含 这个 T 类型
                {
                    HashtableForm.Add(type, Formtemplet);               //在Hashtable 记录 此类型 和 此类型的对象
                    Formtemplet.Show();
                    Formtemplet.FormClosed += new FormClosedEventHandler(Formtemplet_FormClosed);//创建 T 对象的关闭事件
                }
                else
                {
                    Formtemplet = (Form)HashtableForm[type];               //如果  Hashtable存在实例键值 就 获取实例并设置焦点 
                    Formtemplet.Activate();
                }
            }
        }
        #endregion


        T CreateFormHTClass<T>()
        {
          //
Assembly assembly
= Assembly.Load("反射"); Type type = typeof(T); T templet = default(T); { if (!HashtableForm.Contains(type)) { templet = (T)Activator.CreateInstance(type); HashtableForm.Add(type, templet); } else { templet = (T)HashtableForm[type]; } } return templet; } void CreateFormHT(string FormClassName) { Assembly assembly = Assembly.Load("反射"); Type type = assembly.GetType("反射." + FormClassName); Form Formtemplet = (Form)Activator.CreateInstance(type); { if (!HashtableForm.Contains(type)) { HashtableForm.Add(type, Formtemplet); Formtemplet.Show(); Formtemplet.FormClosed += new FormClosedEventHandler(Formtemplet_FormClosed); } else { Formtemplet = (Form)HashtableForm[type]; Formtemplet.Activate(); } } } private void button2_Click(object sender, EventArgs e) { CreateFormHT<Form2>(); } } }
原文地址:https://www.cnblogs.com/bingguang/p/3184220.html