Ejemplo n.º 1
0
        /// <summary>
        ///  初始化对象池
        /// </summary>
        /// <param name="type"></param>
        /// <param name="nInitSize"></param>
        /// <param name="nCapacity"></param>
        /// <param name="objDefaultCreateParam"></param>
        /// <returns></returns>
        public bool InitPool(Type type, Int32 nInitSize, Int32 nCapacity, System.Object[] objDefaultCreateParam)
        {
            if (nInitSize < 0 || nCapacity < 1 || nInitSize > nCapacity)
            {
                BTDebug.Exception("<BT> Object Pool Init Exception");
                return(false);
            }
            m_nCapacity             = nCapacity;
            m_ObjectType            = type;
            m_objDefaultCreateParam = objDefaultCreateParam;
            m_ItemTable             = new Hashtable(nCapacity);
            m_FreeIndexArray        = new ArrayList();
            m_UsingIndexArray       = new ArrayList();
            if (type.IsAssignableFrom(typeof(IRecycleable)) == true)
            {
                m_bSupportRecycle = true;
            }
            else
            {
                m_bSupportRecycle = false;
            }

            for (Int32 i = 0; i < nInitSize; ++i)
            {
                ExtendPool();
            }

#if BTDEBUG
            m_ItemUseFrequencyTable = new Hashtable(nCapacity);
#endif
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="objEntity"></param>
        /// <param name="objParamArray"></param>
        /// <param name="rOutReturnValue"></param>
        /// <returns></returns>
        public bool InvokeMethod(string strName, System.Object objEntity, System.Object[] objParamArray, out System.Object rOutReturnValue)
        {
            rOutReturnValue = default(System.Object);
            if (objEntity == null)
            {
                return(false);
            }

            MethodInfo info = null;

            if (m_MethodInfoList.QuickFind(strName, ref info) == false || info == null)
            {
                return(false);
            }
            if (objEntity.GetType() != info.DeclaringType &&
                objEntity.GetType().IsAssignableFrom(info.DeclaringType) == false)
            {
                return(false);
            }

            try
            {
                rOutReturnValue = info.Invoke(objEntity, objParamArray);
            }
            catch (System.Exception ex)
            {
                BTDebug.Exception(ex);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
0
        private bool ExtendPool()
        {
            lock (this)
            {
                if (Count() >= m_nCapacity)
                {
                    return(false);
                }

                System.Object newObject = null;
                try
                {
                    newObject = System.Activator.CreateInstance(m_ObjectType, m_objDefaultCreateParam);
                }
                catch (System.Exception ex)
                {
                    BTDebug.Exception(ex);
                    return(false);
                }

                Int32 nHashCode = newObject.GetHashCode();
                m_ItemTable.Add(nHashCode, newObject);
                m_FreeIndexArray.Add(nHashCode);
                return(true);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="objEntity"></param>
        /// <param name="rOutValue"></param>
        /// <returns></returns>
        public bool GetValue(string strName, System.Object objEntity, out System.Object rOutValue)
        {
            rOutValue = default(System.Object);
            if (objEntity == null)
            {
                return(false);
            }

            PropertyInfo info = null;

            if (m_PropertyInfoList.QuickFind(strName, ref info) == false || info == null)
            {
                return(false);
            }
            if (objEntity.GetType() != info.DeclaringType &&
                objEntity.GetType().IsAssignableFrom(info.DeclaringType) == false)
            {
                return(false);
            }
            if (info.CanRead == false)
            {
                return(false);
            }
            try
            {
                rOutValue = info.GetValue(objEntity, null);
            }
            catch (System.Exception ex)
            {
                BTDebug.Exception(ex);
                return(false);
            }
            return(true);;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="objEntity"></param>
        /// <param name="objValue"></param>
        /// <returns></returns>
        public bool SetValue(string strName, System.Object objEntity, System.Object objValue)
        {
            if (objEntity == null)
            {
                return(false);
            }

            PropertyInfo info = null;

            if (m_PropertyInfoList.QuickFind(strName, ref info) == false || info == null)
            {
                return(false);
            }
            if (objEntity.GetType() != info.DeclaringType &&
                objEntity.GetType().IsAssignableFrom(info.DeclaringType) == false)
            {
                return(false);
            }
            if (info.CanWrite == false)
            {
                return(false);
            }
            try
            {
                info.SetValue(objEntity, objValue, null);
            }
            catch (System.Exception ex)
            {
                BTDebug.ExceptionEx(ex);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 开始计时
 /// </summary>
 public void StartNewCall()
 {
     if (m_stopwatch.IsRunning == true)
     {
         BTDebug.Warning("<BTDEBUG> Start A CPU Profiler While It is Running, Result will not be correct");
     }
     m_stopwatch.Start();
     m_nCallTime += 1;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="nCacheSetSize"></param>
 /// <param name="handleReleaseCache"></param>
 public CObjectCache(Int32 nCacheSetSize, HandleObjectAction handleReleaseCache = null)
 {
     if (nCacheSetSize <= 0)
     {
         BTDebug.Exception("Object Cache Exception", "BT");
         return;
     }
     m_nCacheSetSize      = nCacheSetSize;
     m_Cache              = new QuickList <string, CCacheSet>();
     m_HandleReleaseCache = handleReleaseCache;
 }
Ejemplo n.º 8
0
        // 初始化方法数据
        private bool InitMethodRelfector(Type type)
        {
            if (type == null || type.IsClass == false)
            {
                return(false);
            }
            if (m_MethodInfoList == null)
            {
                return(false);
            }
            MethodInfo[] methodInfoArray = type.GetMethods();
            Int32        nPropSize       = methodInfoArray == null ? 0 : methodInfoArray.Length;

            for (Int32 i = 0; i < nPropSize; ++i)
            {
                MethodInfo info = methodInfoArray[i];
                if (info == null)
                {
                    continue;
                }
                CMethodAttribute[] cusAttrArray = info.GetCustomAttributes(typeof(CMethodAttribute), false) as CMethodAttribute[];
                Int32 nAttrSize = cusAttrArray == null ? 0 : cusAttrArray.Length;
                for (Int32 nAttrIndex = 0; nAttrIndex < nAttrSize; ++nAttrIndex)
                {
                    CMethodAttribute attribute = cusAttrArray[nAttrIndex];
                    if (attribute == null)
                    {
                        continue;
                    }
                    string strName = attribute.GetName();
                    if (string.IsNullOrEmpty(strName) == true ||
                        m_MethodInfoList.Add(strName, info) == false)
                    {
                        BTDebug.Warning(string.Format("<BTRELFECT> Type:{0} Add Method Info:{1} Failed", type.Name, strName));
                        continue;
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 9
0
        // 初始化属性数据
        private bool InitPropertyRelfector(Type type)
        {
            if (type == null || type.IsClass == false)
            {
                return(false);
            }
            if (m_PropertyInfoList == null)
            {
                return(false);
            }
            PropertyInfo[] propertyInfoArray = type.GetProperties();
            Int32          nPropSize         = propertyInfoArray == null ? 0 : propertyInfoArray.Length;

            for (Int32 i = 0; i < nPropSize; ++i)
            {
                PropertyInfo info = propertyInfoArray[i];
                if (info == null)
                {
                    continue;
                }
                CPropertyAttribute[] cusAttrArray = info.GetCustomAttributes(typeof(CPropertyAttribute), false) as CPropertyAttribute[];
                Int32 nAttrSize = cusAttrArray == null ? 0 : cusAttrArray.Length;
                for (Int32 nAttrIndex = 0; nAttrIndex < nAttrSize; ++nAttrIndex)
                {
                    CPropertyAttribute attribute = cusAttrArray[nAttrIndex];
                    string             strName   = attribute.GetName();
                    if (string.IsNullOrEmpty(strName) == true ||
                        m_PropertyInfoList.Add(strName, info) == false)
                    {
                        BTDebug.Warning(string.Format("Type:{0} Add Property Info:{1} Failed", type.Name, strName), "RELFECT");
                        continue;
                    }
                }
            }
            return(true);
        }