示例#1
0
        public static object TryGetFromPool(this Type type)
        {
#if ENABLE_OBJ_POOL
            if (type != null)
            {
                if (_Pool == null)
                {
                    _Pool = new Dictionary <Type, IInstancePool>();
                }
                IInstancePool pool;
                if (!_Pool.TryGetValue(type, out pool))
                {
                    pool        = new CommonInstancePool();
                    _Pool[type] = pool;
                }
                var list = pool.Pool;
                if (list.Count > 0)
                {
                    var rv = list[list.Count - 1];
                    list.RemoveAt(list.Count - 1);
                    return(rv);
                }
            }
#endif
            return(null);
        }
示例#2
0
        public static object GetFromPool(this Type type, Func <object> funcCreate, Action <object> funcInit)
        {
#if ENABLE_OBJ_POOL
            if (type != null)
            {
                if (_Pool == null)
                {
                    _Pool = new Dictionary <Type, IInstancePool>();
                }
                IInstancePool pool;
                if (!_Pool.TryGetValue(type, out pool))
                {
                    pool        = new CommonInstancePool();
                    _Pool[type] = pool;
                }
                var list = pool.Pool;
                if (list.Count > 0)
                {
                    var rv = list[list.Count - 1];
                    list.RemoveAt(list.Count - 1);
                    if (funcInit != null)
                    {
                        funcInit(rv);
                    }
                    return(rv);
                }
                else
                {
                    if (funcCreate != null)
                    {
                        return(funcCreate());
                    }
                    else
                    {
                        return(Activator.CreateInstance(type));
                    }
                }
            }
            return(null);
#else
            if (type != null)
            {
                if (funcCreate != null)
                {
                    return(funcCreate());
                }
                else
                {
                    return(Activator.CreateInstance(type));
                }
            }
            return(null);
#endif
        }
示例#3
0
 public static void ReturnToPool(this object obj)
 {
     if (obj != null)
     {
         if (!(obj is ValueType))
         {
             var type = obj.GetType();
             if (_Pool == null)
             {
                 _Pool = new Dictionary <Type, IInstancePool>();
             }
             IInstancePool pool;
             if (!_Pool.TryGetValue(type, out pool))
             {
                 pool        = new CommonInstancePool();
                 _Pool[type] = pool;
             }
             pool.Pool.Add(obj);
         }
     }
 }