示例#1
0
        static ObjectPool()
        {
            var setting = new ObjectPoolSettingItem()
            {
                PoolType  = typeof(MemoryStream),
                Creator   = () => new MemoryStream(),
                EnterPool = c =>
                {
                    ((MemoryStream)c).SetLength(0);
                    return(true);
                }
            };

            _StaticRegisters.TryAdd(setting.PoolType, setting);
            setting = new ObjectPoolSettingItem()
            {
                PoolType  = typeof(StringBuilder),
                Creator   = () => new StringBuilder(),
                EnterPool = c =>
                {
                    ((StringBuilder)c).Clear();
                    return(true);
                }
            };
            _StaticRegisters.TryAdd(setting.PoolType, setting);
        }
示例#2
0
        static ObjectPoolSettingItem OnPoolRegister()
        {
            var result = new ObjectPoolSettingItem()
            {
                PoolType  = typeof(ClearHelper),
                Creator   = () => new ClearHelper(),
                EnterPool = c =>
                {
                    ClearHelper clearHelper = (ClearHelper)c;
                    clearHelper._Actions.Clear();
                    clearHelper.disposedValue = false;
                    return(true);
                },
            };

            return(result);
        }
示例#3
0
        static public ObjectPoolSettingItem GetDefaultSetting(Type type)
        {
            ObjectPoolSettingItem setting;
            var mi = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).SingleOrDefault(c => typeof(ObjectPoolSettingItem) == c.ReturnType && null != Attribute.GetCustomAttribute(c, typeof(OnPoolRegisterAttribute)));

            if (null != mi)
            {
                setting = (ObjectPoolSettingItem)mi.Invoke(null, Array.Empty <object>());
            }
            else
            {
                setting = new ObjectPoolSettingItem()
                {
                    PoolType = type,
                    Creator  = () => TypeDescriptor.CreateInstance(null, type, null, null),
                };
                mi = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(c => typeof(bool) == c.ReturnType && null != Attribute.GetCustomAttribute(c, typeof(OnPoolEnteringAttribute)));
                if (null != mi)
                {
                    setting.EnterPool = c => (bool)mi.Invoke(c, Array.Empty <object>());
                }
            }
            return(setting);
        }
示例#4
0
        /// <summary>
        /// 提前静态注册的类型。
        /// </summary>
        /// <param name="settingItem">注册之后再修改此对象引用不会导致行为变化,此函数使用深表复制记录一个副本。</param>
        /// <returns>成功注册则返回true,已经注册则返回false。</returns>
        static public bool Register(ObjectPoolSettingItem settingItem)
        {
            var result = _StaticRegisters.TryAdd(settingItem.PoolType, (ObjectPoolSettingItem)settingItem.Clone());

            return(result);
        }