Exemplo n.º 1
0
        /// <summary>
        /// Returns the value stored in the cache item.
        /// </summary>
        /// <remarks>
        /// This value must be serializable, otherwise <see cref="ArgumentException"/>
        /// is thrown when you will try to add or insert the CacheItem.
        /// </remarks>
        /// <typeparam name="T">Specifies the type of value obtained from the cache item.</typeparam>
        /// <returns>Value of the cache item with the type defined.</returns>
        /// <example>
        /// Example gets value of a cache item
        /// <code>
        /// CacheItem item = cache.GetCacheItem("Product0");
        ///
        /// Product product = item.GetValue&lt;Product&gt;();
        /// </code>
        /// </example>
        public T GetValue <T>()
        {
            if (_value is ValueEmissary)
            {
                if (_currentValue != default(object) && _currentValue is T)
                {
                    return((T)_currentValue);
                }

                var emissary = _value as ValueEmissary;

                if (CacheInstance == default(Cache))
                {
                    return((T)emissary.Data);
                }
                _currentValue = CacheInstance.SafeDeserialize <T>(emissary.Data, CacheInstance.SerializationContext, FlagMap, UserObjectType.CacheItem);

                return((T)_currentValue);
            }
            return((T)_value);
        }