码迷,mamicode.com
首页 > Windows程序 > 详细

C#经典单例

时间:2016-01-12 01:13:31      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 
 3 public class Singleton<T> where T : class, new()
 4 {
 5     private static T _Instance;
 6 
 7     public static T Instance
 8     {
 9         get
10         {
11             if (Singleton<T>._Instance == null)
12             {
13                 Singleton<T>._Instance = Activator.CreateInstance<T>();
14             }
15             return Singleton<T>._Instance;
16         }
17     }
18 
19     static Singleton()
20     {
21         Singleton<T>._Instance = Activator.CreateInstance<T>();
22     }
23 
24     public static void CreateInstance()
25     {
26         if (Singleton<T>._Instance == null)
27         {
28             Singleton<T>._Instance = Activator.CreateInstance<T>();
29         }
30     }
31 
32     public static void DestroyInstance()
33     {
34         if (Singleton<T>._Instance != null)
35         {
36             Singleton<T>._Instance = (T)((object)null);
37         }
38     }
39 
40     public static T GetInstance()
41     {
42         if (Singleton<T>._Instance == null)
43         {
44             Singleton<T>._Instance = Activator.CreateInstance<T>();
45         }
46         return Singleton<T>._Instance;
47     }
48 }

 

C#经典单例

标签:

原文地址:http://www.cnblogs.com/yuxk/p/5123065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!