コード例 #1
0
        private static bool FromStringToTypeProxy(string from, object defaultVal, Type xType, out object result)
        {
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromStringToNumericType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsGuidType(xType))
            {
                return(FromStringToGuid(from, defaultVal, out result));
            }
            if (TypeDeterminer.IsDateTimeTypes(xType))
            {
                return(FromStringToDateTimeType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsEnumType(xType))
            {
                return(FromStringToEnum(from, xType, defaultVal, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = from;
                return(true);
            }

            var _ = TypeDeterminer.GetDefaultValue(xType);

            result = from.Is(xType, t => _ = t) ? _ : defaultVal;
            return(false);
        }
コード例 #2
0
 /// <summary>
 /// Cast <see cref="object"/> to given type.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="targetType"></param>
 /// <returns></returns>
 public static object CastTo(this object obj, Type targetType)
 {
     CastTypeHelper.Guard(targetType, nameof(targetType));
     return(obj is null
         ? default
         : XConv.To(obj, obj.GetType(), targetType, TypeDeterminer.GetDefaultValue(targetType)));
 }
コード例 #3
0
        private static bool FromStringToNullableTypeProxy(string from, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromStringToNullableNumericType(from, innerType, out result));
            }
            if (TypeDeterminer.IsGuidType(innerType))
            {
                return(FromStringToNullableGuid(from, out result));
            }
            if (TypeDeterminer.IsDateTimeTypes(innerType))
            {
                return(FromStringToNullableDateTimeType(from, innerType, out result));
            }
            if (TypeDeterminer.IsEnumType(xType))
            {
                return(FromStringToNullableEnum(from, innerType, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = from;
                return(true);
            }

            var _ = TypeDeterminer.GetDefaultValue(xType);

            result = from.Is(xType, t => _ = t) ? _ : null;
            return(false);
        }