/// <summary> /// Convert <paramref name="value"/> of type <paramref name="type"/> to RedisValue /// </summary> /// <param name="value">value to convert to RedisValue</param> /// <param name="type">type of value to convert</param> /// <param name="provider">optional provider controls how value is converted</param> /// <returns>RedisValue contains the underlying value</returns> /// <exception cref="ArgumentNullException">throw if <paramref name="type"/> is null</exception> /// <exception cref="OhmSharpConvertionException">throw if convertion failed</exception> public static RedisValue ConvertTo(object value, Type type, IFormatProvider provider = null) { if (type == null) { throw new ArgumentNullException(nameof(type)); } try { if (_customConverters.ContainsKey(type)) { return(_customConverters[type].ConvertTo(value, provider)); } if (_buildinConverters.ContainsKey(type)) { return(_buildinConverters[type].ConvertTo(value, provider)); } if (EnumConverter.IsEnum(type)) { return(EnumConverter.ConvertTo(value, type, provider)); } if (NullableEnumConverter.IsNullableEnum(type)) { return(NullableEnumConverter.ConvertTo(value, type, provider)); } } catch (Exception ex) { throw new OhmSharpConvertionException(type, string.Format("Failed to convert type '{0}' to RedisValue.", type.FullName), ex); } throw new OhmSharpConvertionException(type, string.Format("Failed to convert type '{0}' to RedisValue.", type.FullName), new NotSupportedException("No supported IRedisValueConverter found.")); }
/// <summary> /// Whether or not <paramref name="type"/> can be converted to or from RedisValue /// </summary> /// <param name="type">type to check if convertable</param> /// <returns>true if type is convertable; otherwose false</returns> public static bool IsConvertable(Type type) { return(_customConverters.ContainsKey(type) || _buildinConverters.ContainsKey(type) || EnumConverter.IsEnum(type) || NullableEnumConverter.IsNullableEnum(type)); }