using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace Developpez.Dotnet.ComponentModel { /// /// Implémente un singleton avec intialisation tardive pour le type spécifié. /// /// Type de l'objet pour lequel le singleton est implémenté public static class Singleton { /// /// Retourne l'instance unique du type T /// public static T Instance { get { return LazyInitializer._instance; } } private class LazyInitializer { // Constructeur statique explicite pour dire au compilateur C# de // ne pas marquer le type comme 'beforefieldinit' static LazyInitializer() { Debug.WriteLine(string.Format("Initializing singleton instance of type '{0}'", typeof(T).FullName)); } internal static readonly T _instance = (T)Activator.CreateInstance(typeof(T), true); } } }