Пример #1
0
        internal static U Convert <T, U>(this T token) where T : VToken
        {
            if (token == null)
            {
                return(default(U));
            }

            if (token is U
                // don't want to cast JValue to its interfaces, want to get the internal value
                && typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
            {
                // HACK
                return((U)(object)token);
            }
            else
            {
                VValue value = token as VValue;
                if (value == null)
                {
                    throw new InvalidCastException($"Cannot cast {token.GetType()} to {typeof(T)}.");
                }

                if (value.Value is U u)
                {
                    return(u);
                }

                Type targetType = typeof(U);

                if (ReflectionUtils.IsNullableType(targetType))
                {
                    if (value.Value == null)
                    {
                        return(default(U));
                    }

                    targetType = Nullable.GetUnderlyingType(targetType);
                }

                if (TryConvertVdf <U>(value.Value, out U resultObj))
                {
                    return(resultObj);
                }

                return((U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture));
            }
        }
Пример #2
0
 public VValue(VValue other)
     : this(other.Value, other.Type)
 {
 }