单例模板未初始化其实例

时间:2014-07-24 05:07:53

标签: c# .net null initialization singleton

我有一个通用的单例模板和一个派生类。从外部访问单例实例时,它返回null。我可以在代码中发现没有错。实际上,在调试时,静态构造函数会分配_Instance字段。但是,当涉及到Instance属性时,该值为null!

消费者使用情况:

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."));
            }
        }
    }
}

虽然我对改进实施的建议持开放态度,但对于此类模板,线程安全和好/坏做法,已经有很多关于SO的问题。非常感谢这里有什么问题。

1 个答案:

答案 0 :(得分:1)

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

应该是

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