zoukankan      html  css  js  c++  java
  • 单列模式(泛型版)

    View Code
     public static class SingletonProvider<T> where T : new()
        {
            
    private static T m_instance;
            
    private static readonly object sync = new object();
            
    /// <summary>
            
    /// 不够构造函数的泛型单列
            
    /// </summary>
            public static T Instance
            {
                
    get
                {
                    
    if (m_instance == null)
                    {
                        
    lock (sync)
                        {
                            
    if (m_instance == null)
                            {
                                
    try
                                {
                                    m_instance 
    = new T();
                                }
                                
    catch
                                {
                                }
                            }
                        }
                    }
                    
    return m_instance;
                }
            }
            
    /// <summary>
            
    /// 带构造函数的泛型单列模式
            
    /// </summary> 
            public static T InstanceForParameter(params object[] objarr)
            {

                
    if (m_instance == null)
                {
                    
    lock (sync)
                    {
                        
    if (m_instance == null)
                        {
                            var t 
    = typeof(T);
                            var tp 
    = new Type[objarr.Length];
                            
    for (int i = 0; i < objarr.Length; i++)
                            {
                                tp[i] 
    = objarr[i].GetType();
                            }
                            
    try
                            {
                                System.Reflection.ConstructorInfo ci 
    = t.GetConstructor(tp);
                                m_instance 
    = (T)ci.Invoke(objarr);
                            }
                            
    catch
                            {
                            }

                        }
                    }
                }
                
    return m_instance;
            }
        }
  • 相关阅读:
    VSCode插件开发全攻略(一)概览
    如何使用JavaScript实现纯前端读取和导出excel文件
    ReactNative学习笔记(七)Navigator的使用
    ReactNative学习笔记(六)集成视频播放
    ReactNative学习笔记(五)踩坑总结
    ReactNative学习笔记(四)热更新和增量更新
    ReactNative学习笔记(三)打包、调试、运行等相关介绍
    ReactNative学习笔记(二)基础进阶
    ReactNative学习笔记(一)环境搭建
    彻底禁用Chrome的“请停用以开发者模式运行的扩展程序”提示
  • 原文地址:https://www.cnblogs.com/jacd/p/1771136.html
Copyright © 2011-2022 走看看