zoukankan      html  css  js  c++  java
  • 我的单件模式

    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>(); } } }
  • 相关阅读:
    delegate
    .net企业级架构实战之1——框架综述
    .net企业级架构实战之3——业务对象建模及codesmith模板
    ubuntu学习小记
    [导入]【翻译】WF从入门到精通(第六章):加载和卸载实例
    专注SilverLight
    常用JS验证函数总结
    星级评分jQuery插件 (转)
    Windows Server 2003安装最新版MSN的方法
    [导入]【翻译】WF从入门到精通(第七章):基本活动的操作
  • 原文地址:https://www.cnblogs.com/bingguang/p/3184220.html
Copyright © 2011-2022 走看看