All pastes #746578 Raw Edit

Untitled

public text v1 · immutable
·published 2007-10-23 10:26 UTC
rendered paste body
    /// <summary>
    /// Provides functionality of creating a singleton instance of a class.
    /// </summary>
    /// <typeparam name="T">The class to create as a singleton. This class must have a private constructor with no parameters.</typeparam>
    //AJ: http://www.codeproject.com/cs/design/GenericSingletonPattern.asp
    public static class ParameterlessSingleton<T>
      where T : class
    {
        /// <summary>
        /// Static constructor for lazy instaniation.
        /// </summary>
        static ParameterlessSingleton()
        {
        }

        /// <summary>
        /// The singleton instance of provided type  T.
        /// </summary>
        public static readonly T Instance =
          typeof(T).InvokeMember(typeof(T).Name,
                                 BindingFlags.CreateInstance |
                                 BindingFlags.Instance |
                                 BindingFlags.NonPublic,
                                 null, null, null) as T;
    }

This is an example property:
        #region SingletonInstance
        public static ExceptionLogger SingletonInstance
        {
            get
            {
                return Singleton<ExceptionLogger>.Instance;
            }
        }
        #endregion