未初始化其实例的单例模板

本文关键字:单例模 实例 初始化 | 更新日期: 2023-09-27 18:14:25

我有一个泛型单例模板和一个派生类。当从外部访问单例实例时,它返回null。我在代码中没有发现任何错误。实际上,在调试时,静态构造函数会分配_Instance字段。然而,当涉及到Instance属性时,值为空!

消费者使用方法:

var value = Consumer.Instance.SomeProperty;

单例模板和消费者:

namespace SingletonExample
{
    using System;
    using System.Linq;
    using System.Reflection;
    public sealed class Consumer:
        Singleton<Consumer>
    {
        private Consumer ()
        {
        }
        public bool SomeProperty { get { return (true); } }
    }
    public abstract class Singleton<T>
        where T: Singleton<T>
    {
        protected Singleton ()
        {
            Singleton<T>.ThrowOnInCompatibleImplementation();
        }
        private static readonly T _Instance = null;
        static Singleton ()
        {
            Singleton<T>.ThrowOnInCompatibleImplementation();
            Singleton<T> _Instance = (T) Activator.CreateInstance(type : typeof(T), nonPublic : true);
        }
        public static T Instance { get { return (Singleton<T>._Instance); } }
        private static void ThrowOnInCompatibleImplementation ()
        {
            if (!typeof(T).IsSealed)
            {
                // Force derived classes to be sealed.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must be sealed."));
            }
            if (typeof(T).GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Any())
            {
                // Disallow derived classes to implement static constructors.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must not have static constructors."));
            }
            if (typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.Public).Any())
            {
                // Disallow derived classes to implement instance public constructors.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must not have public constructors."));
            }
            if (typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).Any(ctor => !ctor.IsPrivate))
            {
                // Disallow derived classes to implement instance protected constructors.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must not have protected constructors."));
            }
            if (!typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).Any())
            {
                // Force derived classes to implement a private parameterless constructor.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must have a private parameterless constructor."));
            }
            if (typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).Any(ctor => ctor.GetParameters().Length != 0))
            {
                // Force derived classes to implement a private parameterless constructor.
                throw (new InvalidOperationException("Classes derived from [Singleton<T>] must have a private parameterless constructor."));
            }
        }
    }
}

虽然我对改进实现的建议非常开放,但是关于这样的模板,线程安全和好/坏实践,已经有大量的问题。如果你能告诉我这里出了什么问题,我将不胜感激。

未初始化其实例的单例模板

 Singleton<T> _Instance = (T) Activator.CreateInstance(type : typeof(T), nonPublic : true);
应该

_Instance = (T) Activator.CreateInstance(type : typeof(T), nonPublic : true);