Ejemplo n.º 1
0
 public StringConvBeDecimalTypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
Ejemplo n.º 2
0
        private static bool FromDateTimeToNullableString(DateTime dateTime, CastingContext context, out object result)
        {
            var format = context.Format;

            if (string.IsNullOrWhiteSpace(format))
            {
                format = "yyyy-MM-dd HH:mm:ss";
            }

            result = context.DateTimeFormatStyles switch
            {
                DateTimeFormatStyles.Normal => dateTime.ToString(format, context.FormatProvider),
                DateTimeFormatStyles.Date => dateTime.ToString(DateTimeOutputStyles.Date),
                DateTimeFormatStyles.Time => dateTime.ToString(DateTimeOutputStyles.Time),
                DateTimeFormatStyles.DateTime => dateTime.ToString(DateTimeOutputStyles.DateTime),
                DateTimeFormatStyles.LongDate => dateTime.ToString(DateTimeOutputStyles.LongDate),
                DateTimeFormatStyles.LongTime => dateTime.ToString(DateTimeOutputStyles.LongTime),
                DateTimeFormatStyles.ShortDate => dateTime.ToString(DateTimeOutputStyles.ShortDate),
                DateTimeFormatStyles.ShortTime => dateTime.ToString(DateTimeOutputStyles.ShortTime),
                DateTimeFormatStyles.Local => dateTime.ToLocalTime().ToString(format, context.FormatProvider),
                DateTimeFormatStyles.Universal => dateTime.ToUniversalTime().ToString(format, context.FormatProvider),
                _ => dateTime.ToString(format, context.FormatProvider)
            };

            return(true);
        }
    }
Ejemplo n.º 3
0
 public StringConvBeEncodingTypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
        public static bool Is(this string text, Type type, Action <CastingContext> contextAct, Action <object> matchedCallback = null)
        {
            var context = new CastingContext();

            contextAct?.Invoke(context);
            return(text.Is(type, context, matchedCallback));
        }
Ejemplo n.º 5
0
 public StringConvBeInt32TypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
Ejemplo n.º 6
0
 public StringConvBeDateTimeOffsetTypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
 public StringConvBeTimeSpanTypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
        public static object FromObjTo(object fromObj, Type oType, CastingContext context, Type xType, bool xTypeNullableFlag, object defaultVal = default)
        {
            object result;

            var valueUpdated = xTypeNullableFlag
                ? FromObjToNullableTypeProxy(fromObj, xType, out result)
                : FromObjToTypeProxy(fromObj, defaultVal, xType, out result);

            if (valueUpdated)
            {
                return(result);
            }

            try
            {
                return(Convert.ChangeType(fromObj, xType, context.FormatProvider).AsOrDefault(defaultVal));
            }
            catch
            {
                try
                {
                    return(DefaultMapper.Instance.MapTo(oType, xType, fromObj));
                }
                catch
                {
                    return(xTypeNullableFlag ? default : defaultVal);
                }
            }
        }
Ejemplo n.º 9
0
        public static X FromObjTo <X>(object fromObj, CastingContext context, Type xType, bool xTypeNullableFlag, X defaultVal = default)
        {
            bool   valueUpdated;
            object result;

            if (xTypeNullableFlag)
            {
                valueUpdated = FromObjToNullableTypeProxy(fromObj, xType, out result);
            }
            else
            {
                valueUpdated = FromObjToTypeProxy(fromObj, defaultVal, xType, out result);
            }

            if (valueUpdated)
            {
                return((X)result);
            }

            try
            {
                return(Convert.ChangeType(fromObj, xType, context.FormatProvider).AsOrDefault(defaultVal));
            }
            catch
            {
                try
                {
                    return(Mapper.DefaultMapper.Instance.MapTo <object, X>(fromObj));
                }
                catch
                {
                    return(xTypeNullableFlag ? default : defaultVal);
                }
            }
        }
Ejemplo n.º 10
0
 public StringConvBeVersionTypeTests()
 {
     Context = new CastingContext
     {
         IgnoreCase = IgnoreCase.TRUE
     };
 }
Ejemplo n.º 11
0
 public static string GuidToString(Guid guid, CastingContext context, string defaultVal = "")
 {
     try
     {
         return(guid.ToString(context.GuidFormatStyles.X(), context.FormatProvider));
     }
     catch
     {
         return(defaultVal);
     }
 }
Ejemplo n.º 12
0
 public static string GuidToString(Guid?guid, CastingContext context)
 {
     try
     {
         return(guid.SafeGuid()?.ToString(context.GuidFormatStyles.X(), context.FormatProvider) ?? string.Empty);
     }
     catch
     {
         return(string.Empty);
     }
 }
Ejemplo n.º 13
0
        public static object FromNullableGuidTo(Guid?guid, CastingContext context, Type xType, bool xTypeNullableFlag, object defaultVal = default)
        {
            if (guid.HasValue)
            {
                return(FromGuidTo(guid.Value, context, xType, xTypeNullableFlag, defaultVal));
            }

            return(xTypeNullableFlag
                ? default
                : defaultVal);
        }
 private static bool FromBooleanToNumericType <N>(bool oVal, CastingContext context, Type xType, out object result)
 {
     if (xType == TypeClass.ByteClass)
     {
         result = oVal.ToBinary();
     }
     else
     {
         result = oVal ? context.NumericTrue.As <N>() : context.NumericFalse.As <N>();
     }
     return(true);
 }
Ejemplo n.º 15
0
        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 = StringConv.Int16ToString(_1, defaultStr);
                break;

            case ushort _2:
                result = StringConv.UInt16ToString(_2, defaultStr);
                break;

            case int _3:
                result = StringConv.Int32ToString(_3, defaultStr);
                break;

            case uint _4:
                result = StringConv.UInt32ToString(_4, defaultStr);
                break;

            case long _5:
                result = StringConv.Int64ToString(_5, defaultStr);
                break;

            case ulong _6:
                result = StringConv.UInt64ToString(_6, defaultStr);
                break;

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

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

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

            default:
                valueUpdated = false;
                break;
            }

            return(valueUpdated);
        }
        private static bool FromBooleanToNullableNumericType(bool oVal, CastingContext context, Type innerType, out object result)
        {
            if (innerType == TypeClass.ByteClass)
            {
                result = oVal.ToBinary();
            }
            else
            {
                var midNumeric = oVal ? context.NumericTrue : context.NumericFalse;
                result = Convert.ChangeType(midNumeric, innerType);
            }

            return(true);
        }
Ejemplo n.º 17
0
        private static bool FromBooleanToNumericType(bool oVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsByteClass(xType))
            {
                result = oVal.ToBinary();
            }
            else
            {
                var midNumeric = oVal ? context.NumericTrue : context.NumericFalse;
                result = Convert.ChangeType(midNumeric, xType);
            }

            return(true);
        }
Ejemplo n.º 18
0
        public static X FromEnumTo <X>(Type enumType, object enumVal, CastingContext context, Type xType, bool xTypeNullableFlag, X defaultVal = default)
        {
            object result;

            var valueUpdated = xTypeNullableFlag
                ? FromEnumToNullableTypeProxy(enumType, enumVal, context, xType, out result)
                : FromEnumToTypeProxy(enumType, enumVal, defaultVal, context, xType, out result);

            return(valueUpdated
                ? (X)result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
        public static X FromDateTimeTo <X>(DateTime dateTime, CastingContext context, Type xType, bool xTypeNullableFlag, X defaultVal = default)
        {
            object result;

            var valueUpdated = xTypeNullableFlag
                ? FromDateTimeToNullableTypeProxy(dateTime, context, xType, out result)
                : FromDateTimeToTypeProxy(dateTime, defaultVal, context, xType, out result);

            return(valueUpdated
                ? (X)result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
Ejemplo n.º 20
0
        public static object FromGuidTo(Guid guid, CastingContext context, Type xType, bool xTypeNullableFlag, object defaultVal = default)
        {
            object result;

            var valueUpdated = xTypeNullableFlag
                ? FromGuidToNullableTypeProxy(guid, context, xType, out result)
                : FromGuidToTypeProxy(guid, defaultVal, context, xType, out result);

            return(valueUpdated
                ? result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
Ejemplo n.º 21
0
        private static bool FromGuidToTypeProxy(Guid from, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (defaultVal is string defaultStr)
            {
                return(FromGuidToString(from, context, defaultStr, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = from;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
Ejemplo n.º 22
0
        private static bool FromGuidToTypeProxy(Guid from, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (defaultVal is string defaultStr)
            {
                return(FromGuidToString(from, context, defaultStr, out result));
            }
            if (xType == TypeClass.ObjectClass)
            {
                result = from;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
        public static X FromNumericTo <X>(
            Type oType, bool oTypeNullableFlag, object oVal, CastingContext context,
            Type xType, bool xTypeNullableFlag, X defaultVal = default)
        {
            object result;

            var valueUpdated = oTypeNullableFlag
                ? FromNullableNumericTypeToTypeProxy(oType, oVal, context, xType, out result)
                : FromNumericTypeToTypeProxy(oVal, defaultVal, context, xType, out result);

            return(valueUpdated
                ? (X)result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
Ejemplo n.º 24
0
        private static bool FromGuidToNullableTypeProxy(Guid from, CastingContext context, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (innerType == TypeClass.StringClazz)
            {
                return(FromGuidToString(from, context, string.Empty, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = from;
                return(true);
            }

            result = null;
            return(false);
        }
Ejemplo n.º 25
0
        private static bool FromGuidToNullableTypeProxy(Guid from, CastingContext context, Type xType, out object result)
        {
            var innerType = Nullable.GetUnderlyingType(xType);

            if (innerType == TypeClass.StringClass)
            {
                return(FromGuidToString(from, context, string.Empty, out result));
            }
            if (innerType == TypeClass.ObjectClass)
            {
                result = from;
                return(true);
            }

            result = null;
            return(false);
        }
Ejemplo n.º 26
0
        private static bool FromBooleanToTypeProxy(bool oVal, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(xType))
            {
                return(FromBooleanToString(oVal, context, out result));
            }
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromBooleanToNumericType(oVal, context, xType, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = oVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
Ejemplo n.º 27
0
        private static bool FromBooleanToTypeProxy(bool oVal, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (xType == TypeClass.StringClass)
            {
                return(FromBooleanToString(oVal, context, out result));
            }
            if (TypeHelper.IsNumericType(xType))
            {
                return(FromBooleanToNumericType(oVal, context, xType, out result));
            }
            if (xType == TypeClass.ObjectClass)
            {
                result = oVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
Ejemplo n.º 28
0
        public static X FromBooleanTo <X>(bool oVal, CastingContext context, Type xType, bool xTypeNullableFlag, X defaultVal = default)
        {
            bool   valueUpdated;
            object result;

            if (xTypeNullableFlag)
            {
                valueUpdated = FromBooleanToNullableTypeProxy(oVal, context, xType, out result);
            }
            else
            {
                valueUpdated = FromBooleanToTypeProxy(oVal, defaultVal, context, xType, out result);
            }

            return(valueUpdated
                ? (X)result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
Ejemplo n.º 29
0
        public static object FromEnumTo(Type enumType, object enumVal, CastingContext context, Type xType, bool xTypeNullableFlag, object defaultVal = default)
        {
            bool   valueUpdated;
            object result;

            if (xTypeNullableFlag)
            {
                valueUpdated = FromEnumToNullableTypeProxy(enumType, enumVal, context, xType, out result);
            }
            else
            {
                valueUpdated = FromEnumToTypeProxy(enumType, enumVal, defaultVal, context, xType, out result);
            }

            return(valueUpdated
                ? result
                : xTypeNullableFlag
                    ? default
                    : defaultVal);
        }
        private static bool FromDateTimeToNullableTypeProxy(DateTime dateTime, CastingContext context, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsStringType(innerType))
            {
                return(FromDateTimeToNullableString(dateTime, context, out result));
            }
            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromDateTimeToNullableNumericType(dateTime, innerType, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = dateTime;
                return(true);
            }

            result = null;
            return(false);
        }