• 单例模式


    using System;
    using System.Linq;
    using System.Reflection;
    
    namespace DotnetSpider.Core.Infrastructure
    {
    	/// <summary>
    	/// 单独的泛型实型
    	/// </summary>
    	/// <typeparam name="T"></typeparam>
    	public abstract class Singleton<T>
    	{
    		private static readonly Lazy<T> MyInstance = new Lazy<T>(() =>
    		{
    			var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    			if (ctors.Length != 1)
    			{
    				throw new InvalidOperationException($"Type {typeof(T)} must have exactly one constructor.");
    			}
    			var ctor = ctors.SingleOrDefault(c => !c.GetParameters().Any() && c.IsPrivate);
    			if (ctor == null)
    			{
    				throw new InvalidOperationException($"The constructor for {typeof(T)} must be private and take no parameters.");
    			}
    			return (T)ctor.Invoke(null);
    		});
    
    		/// <summary>
    		/// 单例对象
    		/// </summary>
    		public static T Instance => MyInstance.Value;
    	}
    }
    

    源码

  • 相关阅读:
    放缩ImageView
    2017/5/3 afternoon
    2017/5/3 morning
    2017/5/2 afternoon
    2017/5/2 morning
    2017/4/28 afternoon
    2017/4/28 morning
    2017/4/27 afternoon
    2017/4/27 morning
    2017/4/26 afternoon
  • 原文地址:https://www.cnblogs.com/TTonly/p/11069872.html
Copyright © 2020-2023  润新知