Ejemplo n.º 1
0
        public static TEnum?ObjToNullableEnum <TEnum>(object obj) where TEnum : struct, Enum
        {
            var enumStr = StrConvX.ObjectSafeToString(obj);

            if (StringEnumDeterminer <TEnum> .Is(enumStr))
            {
                return(StringEnumDeterminer <TEnum> .To(enumStr));
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static object ObjToNullableEnum(object obj, Type enumType)
        {
            var enumStr = StrConvX.ObjectSafeToString(obj);

            if (StringEnumDeterminer.Is(enumStr, enumType))
            {
                return(StringEnumDeterminer.To(enumStr, enumType));
            }
            return(null);
        }
        private static bool FromNumericTypeToString <N>(N numericVal, CastingContext context, string defaultStr, out object result)
        {
            var valueUpdated = true;

            result = defaultStr;

            switch (numericVal)
            {
            case short _1:
                result = StrConvX.Int16ToString(_1, context.NumericConvOptions, defaultStr);
                break;

            case ushort _2:
                result = StrConvX.UInt16ToString(_2, context.NumericConvOptions, defaultStr);
                break;

            case int _3:
                result = StrConvX.Int32ToString(_3, context.NumericConvOptions, defaultStr);
                break;

            case uint _4:
                result = StrConvX.UInt32ToString(_4, context.NumericConvOptions, defaultStr);
                break;

            case long _5:
                result = StrConvX.Int64ToString(_5, context.NumericConvOptions, defaultStr);
                break;

            case ulong _6:
                result = StrConvX.UInt64ToString(_6, context.NumericConvOptions, defaultStr);
                break;

            case float _7:
                result = StrConvX.FloatToString(_7, context.Digits, context.NumericConvOptions);
                break;

            case double _8:
                result = StrConvX.DoubleToString(_8, context.Digits, context.NumericConvOptions);
                break;

            case decimal _9:
                result = StrConvX.DecimalToString(_9, context.Digits, context.NumericConvOptions, defaultStr);
                break;

            default:
                valueUpdated = false;
                break;
            }

            return(valueUpdated);
        }
        private static bool FromNullableNumericTypeToString(object numericVal, Type oType, CastingContext context, out object result)
        {
            var valueUpdated = true;

            result = null;

            if (oType == TypeClass.Int16NullableClazz)
            {
                result = StrConvX.Int16ToString(NumConvX.ObjectToNullableInt16(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.UInt16NullableClazz)
            {
                result = StrConvX.UInt16ToString(NumConvX.ObjectToNullableUInt16(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.Int32NullableClazz)
            {
                result = StrConvX.Int32ToString(NumConvX.ObjectToNullableInt32(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.UInt32NullableClazz)
            {
                result = StrConvX.UInt32ToString(NumConvX.ObjectToNullableUInt32(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.Int64NullableClazz)
            {
                result = StrConvX.Int64ToString(NumConvX.ObjectToNullableInt64(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.UInt64NullableClazz)
            {
                result = StrConvX.UInt64ToString(NumConvX.ObjectToNullableUInt64(numericVal), context.NumericConvOptions, string.Empty);
            }
            else if (oType == TypeClass.FloatNullableClazz)
            {
                result = StrConvX.FloatToString(NumConvX.ObjectToNullableFloat(numericVal), context.Digits, context.NumericConvOptions);
            }
            else if (oType == TypeClass.DoubleNullableClazz)
            {
                result = StrConvX.DoubleToString(NumConvX.ObjectToNullableDouble(numericVal), context.Digits, context.NumericConvOptions);
            }
            else if (oType == TypeClass.DecimalNullableClazz)
            {
                result = StrConvX.DecimalToString(NumConvX.ObjectToNullableDecimal(numericVal), context.Digits, context.NumericConvOptions, string.Empty);
            }
            else
            {
                valueUpdated = false;
            }

            return(valueUpdated);
        }
        private static bool FromObjToNullableTypeProxy(object fromObj, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsStringType(innerType))
            {
                result = StrConvX.ObjectSafeToString(fromObj);
                return(true);
            }

            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromObjToNullableNumericType(fromObj, innerType, out result));
            }

            if (TypeDeterminer.IsEnumType(innerType))
            {
                result = EnumConvX.ObjToNullableEnum(fromObj, innerType);
            }

            if (TypeDeterminer.IsDateTimeTypes(innerType))
            {
                return(FromObjToNullableDateTimeType(fromObj, innerType, out result));
            }

            if (TypeDeterminer.IsGuidType(innerType))
            {
                result = GuidConvX.ObjToNullableGuid(fromObj);
                return(true);
            }

            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = fromObj;
                return(true);
            }

            result = null;
            return(false);
        }
        private static bool FromObjToTypeProxy <X>(object fromObj, X defaultVal, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(defaultVal, out var defaultStr))
            {
                result = StrConvX.ObjectSafeToString(fromObj, defaultStr);
                return(true);
            }

            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromObjToNumericType(fromObj, defaultVal, xType, out result));
            }

            if (TypeDeterminer.IsEnumType(xType))
            {
                result = EnumConvX.ObjToEnum(fromObj, xType, defaultVal);
                return(true);
            }

            if (TypeDeterminer.IsDateTimeTypes(xType))
            {
                return(FromObjToDateTime(fromObj, defaultVal, xType, out result));
            }

            if (defaultVal is Guid defaultGuid)
            {
                result = GuidConvX.ObjToGuid(fromObj, defaultGuid);
                return(true);
            }

            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = defaultVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
Ejemplo n.º 7
0
 public static TEnum ObjToEnum <TEnum>(object obj, TEnum defaultVal = default) where TEnum : struct, Enum
 {
     return(StringEnumDeterminer <TEnum> .To(StrConvX.ObjectSafeToString(obj), defaultVal : defaultVal));
 }
Ejemplo n.º 8
0
 public static object ObjToEnum(object obj, Type enumType, object defaultVal = default)
 {
     return(StringEnumDeterminer.To(StrConvX.ObjectSafeToString(obj), enumType, defaultVal: defaultVal));
 }
 private static bool FromGuidToString(Guid guid, CastingContext context, string defaultStr, out object result)
 {
     result = StrConvX.GuidToString(guid, context, defaultStr);
     return(true);
 }
 private static bool FromNullableGuidToString(Guid?guid, CastingContext context, out object result)
 {
     result = StrConvX.GuidToString(guid, context);
     return(true);
 }