12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Collections.Generic;
- /// <summary>
- /// 普通单例
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class Singleton<T> where T : Singleton<T>, new()
- {
- /// <summary>
- /// 单例
- /// </summary>
- private static T m_Instance;
- /// <summary>
- /// 实例
- /// </summary>
- public static T Instance
- {
- get
- {
- if (object.ReferenceEquals(Singleton<T>.m_Instance, null))
- {
- var obj = new T();
- Singleton<T>.m_Instance = obj;
- Container.Add(typeof(T).Name, obj);
- }
- return Singleton<T>.m_Instance;
- }
- }
- /// <summary>
- /// 新能剖析方法,
- /// </summary>
- /// <returns>返回每个单例占用的内存大小</returns>
- [Obsolete("没有意义,需要不断的遍历引用直到基础类型才能确定最终大小,-wg 2019年10月23日 11:42:21")]
- public static Dictionary<string, int> Profile_Size()
- {
- var dic = new Dictionary<string, int>();
- //foreach (var itc in Container) {
- // // itc.Key
- // System.Runtime.InteropServices.Marshal.SizeOf( itc.Value)
- //}
- return dic;
- }
- #region ' 内部容器 '
- static Dictionary<string, Object> _mC;
- static Dictionary<string, Object> Container
- {
- get
- {
- if (null == _mC)
- {
- _mC = new Dictionary<string, object>();
- }
- return _mC;
- }
- }
- #endregion
- }
|