Beispiel #1
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);
        }
Beispiel #2
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);
        }
        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);
        }
Beispiel #5
0
 public static TEnum ObjToEnum <TEnum>(object obj, TEnum defaultVal = default) where TEnum : struct, Enum
 {
     return(StringEnumDeterminer <TEnum> .To(StrConvX.ObjectSafeToString(obj), defaultVal : defaultVal));
 }
Beispiel #6
0
 public static object ObjToEnum(object obj, Type enumType, object defaultVal = default)
 {
     return(StringEnumDeterminer.To(StrConvX.ObjectSafeToString(obj), enumType, defaultVal: defaultVal));
 }