Example #1
0
        /// <summary>
        /// 更具上下文获取相应的上下文缓冲对象
        /// </summary>
        /// <returns></returns>
        private static NameBasedDictionary GetCache()
        {
            NameBasedDictionary cache;

            if (isWeb)
            {
                cache = (NameBasedDictionary)HttpContext.Current.Items[ContextKey];
            }
            else
            {
                cache = threadCache;
            }
            if (cache == null)
            {
                cache = new NameBasedDictionary();
            }
            if (isWeb)
            {
                HttpContext.Current.Items[ContextKey] = cache;
            }
            else
            {
                threadCache = cache;
            }
            return(cache);
        }
Example #2
0
        /// <summary>
        /// 根据上下文成员名称,返回对应内容
        ///
        /// 由于threadCache或HttpContext中的缓冲对象都在构造过程中创建、
        /// 因此这里没有cache==bull的判断
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    return(null);
                }
                NameBasedDictionary cache = GetCache();
                if (cache.Count <= 0)
                {
                    return(null);
                }
                object result;
                if (cache.TryGetValue(name, out result))
                {
                    return(result);
                }
                else
                {
                    return(null);
                }
            }

            set
            {
                if (string.IsNullOrEmpty(name))
                {
                    return;
                }
                NameBasedDictionary cache = GetCache();
                object temp;
                if (cache.TryGetValue(name, out temp))
                {
                    cache[name] = value;
                }
                else
                {
                    cache.Add(name, value);
                }
            }
        }