Ejemplo n.º 1
0
        public static object ObjToNullableEnum(object obj, Type enumType)
        {
            var enumStr = StringConv.ObjectSafeToString(obj);

            if (StringEnumDeterminer.Is(enumStr, enumType))
            {
                return(StringEnumDeterminer.To(enumStr, enumType));
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static TEnum?ObjToNullableEnum <TEnum>(object obj) where TEnum : struct, Enum
        {
            var enumStr = StringConv.ObjectSafeToString(obj);

            if (StringEnumDeterminer <TEnum> .Is(enumStr))
            {
                return(StringEnumDeterminer <TEnum> .To(enumStr));
            }
            return(null);
        }
Ejemplo n.º 3
0
        private static bool FromObjToNullableTypeProxy(object fromObj, Type xType, out object result)
        {
            var innerType = Nullable.GetUnderlyingType(xType);

            if (innerType == TypeClass.StringClass)
            {
                result = StringConv.ObjectSafeToString(fromObj);
                return(true);
            }

            if (TypeHelper.IsNumericType(xType))
            {
                return(FromObjToNullableNumericType(fromObj, xType, out result));
            }

            if (TypeHelper.IsEnumType(innerType))
            {
                result = EnumConv.ObjToNullableEnum(fromObj, innerType);
            }

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

            if (TypeHelper.IsGuidType(innerType))
            {
                result = GuidConv.ObjToNullableGuid(fromObj);
                return(true);
            }

            if (innerType == TypeClass.ObjectClass)
            {
                result = fromObj;
                return(true);
            }

            result = null;
            return(false);
        }
Ejemplo n.º 4
0
        private static bool FromObjToTypeProxy <X>(object fromObj, X defaultVal, Type xType, out object result)
        {
            if (defaultVal is string defaultStr)
            {
                result = StringConv.ObjectSafeToString(fromObj, defaultStr);
                return(true);
            }

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

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

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

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

            if (xType == TypeClass.ObjectClass)
            {
                result = defaultVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
Ejemplo n.º 5
0
 public static TEnum ObjToEnum <TEnum>(object obj, TEnum defaultVal = default) where TEnum : struct, Enum
 {
     return(StringEnumDeterminer <TEnum> .To(StringConv.ObjectSafeToString(obj), defaultVal : defaultVal));
 }
Ejemplo n.º 6
0
 public static object ObjToEnum(object obj, Type enumType, object defaultVal = default)
 {
     return(StringEnumDeterminer.To(StringConv.ObjectSafeToString(obj), enumType, defaultVal: defaultVal));
 }