2017年1月2日 星期一

設計模式 C# 的 Singleton 的泛型


讓人繼承就可已變成 Singleton 獨體



  1. ///
  2. /// 給人繼承成為獨體
  3. ///
  4. /// 要繼承的類別
  5. public class Singleton where T : class, new()
  6. {
  7. protected Singleton()
  8. {
  9. Debug.Assert(null == _instance);
  10. }
  11. private static readonly T _instance = new T();
  12. public static T Instance
  13. {
  14. get
  15. {
  16. return _instance;
  17. }
  18. }
  19. }
  20.  
  21.  

沒有留言:

Visual Studio 2017/2019 推薦的擴充功能與更新

參考文章: 覺得 Google 的 Blogger 不太順手?透過 HTML 的 iframe 移花接木 HackMD