コード例 #1
0
        /// <summary>
        /// Convert <paramref name="value"/> from RedisValue to type <paramref name="type"/>
        /// </summary>
        /// <param name="value">type of object to return</param>
        /// <param name="type">RedisValue to convert from</param>
        /// <param name="provider">optional provider controls how value is converted</param>
        /// <returns>object contained in the RedisValue</returns>
        /// <exception cref="OhmSharpConvertionException">throw if convertion failed</exception>
        /// <exception cref="ArgumentNullException">throw if <paramref name="type"/> is null</exception>
        public static object ConvertFrom(RedisValue value, Type type, IFormatProvider provider = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            try
            {
                if (_customConverters.ContainsKey(type))
                {
                    return(_customConverters[type].ConvertFrom(value, provider));
                }

                if (_buildinConverters.ContainsKey(type))
                {
                    return(_buildinConverters[type].ConvertFrom(value, provider));
                }

                if (EnumConverter.IsEnum(type))
                {
                    return(EnumConverter.ConvertFrom(value, type, provider));
                }

                if (NullableEnumConverter.IsNullableEnum(type))
                {
                    return(NullableEnumConverter.ConvertFrom(value, type, provider));
                }
            }
            catch (Exception ex)
            {
                throw new OhmSharpConvertionException(type,
                                                      string.Format("Failed to convert type '{0}' from RedisValue", type.FullName), ex);
            }

            throw new OhmSharpConvertionException(type,
                                                  string.Format("Failed to convert type '{0}' from RedisValue.", type.FullName),
                                                  new NotSupportedException("No supported IRedisValueConverter found."));
        }