/// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="context"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T?CastToNullable <T>(this string str, CastingContext context) where T : struct
        {
            T?result = default;

            context ??= CastingContext.DefaultContext;
            return(str.IsNullable <T>(context, v => result = v, () => result = null)? result : null);
        }
        /// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="contextAct"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T?CastToNullable <T>(this string str, Action <CastingContext> contextAct) where T : struct
        {
            T?  result  = default;
            var context = new CastingContext();

            contextAct?.Invoke(context);
            return(str.IsNullable <T>(context, v => result = v, () => result = null)? result : null);
        }
        /// <summary>
        /// Cast to
        /// </summary>
        /// <param name="str"></param>
        /// <param name="contextAct"></param>
        /// <param name="defaultVal"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T CastTo <T>(this string str, Action <CastingContext> contextAct, T defaultVal = default)
        {
            T   result  = default;
            var context = new CastingContext();

            contextAct?.Invoke(context);
            return(str.Is <T>(context, v => result = v) ? result : XConv.To(str, defaultVal, context));
        }
        /// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static object CastToNullable(this string str, Type type, CastingContext context)
        {
            CastTypeHelper.Guard(type, nameof(type));
            object result = default;

            context ??= CastingContext.DefaultContext;
            return(str.IsNullable(type, context, v => result = v, () => result = null)
                ? result
                : null);
        }
        /// <summary>
        /// Cast to
        /// </summary>
        /// <param name="str"></param>
        /// <param name="targetType"></param>
        /// <param name="defaultVal"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static object CastTo(this string str, Type targetType, CastingContext context, object defaultVal = default)
        {
            CastTypeHelper.Guard(targetType, nameof(targetType));
            var result = defaultVal;

            context ??= CastingContext.DefaultContext;
            return(str.Is(targetType, v => result = v)
                ? result
                : XConv.To(str, TypeClass.StringClazz, targetType, defaultVal, context));
        }
        /// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="context"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T?CastToNullable <T>(this string str, CastingContext context = default) where T : struct
        {
            T?result = default;

            context ??= CastingContext.DefaultContext;

            return(str.IsNullable <T>((t) => result = t, () => result = null,
                                      context.IgnoreCase.X(), context.Format, context.NumberStyles, context.DateTimeStyles, context.FormatProvider)
                ? result
                : null);
        }
        /// <summary>
        /// Cast to
        /// </summary>
        /// <param name="str"></param>
        /// <param name="targetType"></param>
        /// <param name="defaultVal"></param>
        /// <param name="contextAct"></param>
        /// <returns></returns>
        public static object CastTo(this string str, Type targetType, Action <CastingContext> contextAct, object defaultVal = default)
        {
            CastTypeHelper.Guard(targetType, nameof(targetType));
            var result  = defaultVal;
            var context = new CastingContext();

            contextAct?.Invoke(context);
            return(str.Is(targetType, v => result = v)
                ? result
                : XConv.To(str, TypeClass.StringClazz, targetType, defaultVal, context));
        }
        /// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static object CastToNullable(this string str, Type type, CastingContext context = null)
        {
            object result = default;

            context ??= CastingContext.DefaultContext;

            return(str.IsNullable(type, (t) => result = t, () => result = null,
                                  context.IgnoreCase.X(), context.Format, context.NumberStyles, context.DateTimeStyles, context.FormatProvider)
                ? result
                : null);
        }
 /// <summary>
 /// Cast to
 /// </summary>
 /// <param name="str"></param>
 /// <param name="targetType"></param>
 /// <param name="defaultVal"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static object CastTo(this string str, Type targetType, CastingContext context, object defaultVal = default) =>
 XConv.To(str, TypeClass.StringClass, targetType, defaultVal, context);
Exemple #10
0
 /// <summary>
 /// Convert from TFrom to TTo
 /// </summary>
 /// <param name="source"></param>
 /// <param name="defaultVal"></param>
 /// <param name="context"></param>
 /// <typeparam name="TFrom"></typeparam>
 /// <typeparam name="TTo"></typeparam>
 /// <returns></returns>
 public static TTo To <TFrom, TTo>(TFrom source, TTo defaultVal, CastingContext context = default) =>
 XConv.To(source, defaultVal, context);
Exemple #11
0
 /// <summary>
 /// Convert from TFrom to TTo
 /// </summary>
 /// <param name="source"></param>
 /// <param name="context"></param>
 /// <typeparam name="TFrom"></typeparam>
 /// <typeparam name="TTo"></typeparam>
 /// <returns></returns>
 public static TTo To <TFrom, TTo>(TFrom source, CastingContext context = default) =>
 XConv.To <TFrom, TTo>(source, context: context);
Exemple #12
0
 /// <summary>
 /// Convert from sourceType to targetType
 /// </summary>
 /// <param name="source"></param>
 /// <param name="defaultVal"></param>
 /// <param name="context"></param>
 /// <typeparam name="TTo"></typeparam>
 /// <returns></returns>
 public static TTo To <TTo>(object source, TTo defaultVal, CastingContext context = default) =>
 XConv.To(source, source?.GetType(), typeof(TTo), defaultVal, context).As <TTo>();
Exemple #13
0
 /// <summary>
 /// Convert from sourceType to targetType
 /// </summary>
 /// <param name="sourceType"></param>
 /// <param name="targetType"></param>
 /// <param name="source"></param>
 /// <param name="defaultVal"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static object To(Type sourceType, Type targetType, object source, object defaultVal, CastingContext context = default) =>
 XConv.To(source, sourceType, targetType, defaultVal, context);
 public static TTo To <TTo>(object source, CastingContext context = default)
 {
     return(XConv.To(source, source?.GetType(), typeof(TTo), context: context).As <TTo>());
 }
        /// <summary>
        /// Cast to
        /// </summary>
        /// <param name="str"></param>
        /// <param name="context"></param>
        /// <param name="defaultVal"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T CastTo <T>(this string str, CastingContext context, T defaultVal = default)
        {
            T result = default;

            return(str.Is <T>(context, v => result = v) ? result : XConv.To(str, defaultVal, context));
        }
 /// <summary>
 /// Cast to
 /// </summary>
 /// <param name="str"></param>
 /// <param name="context"></param>
 /// <param name="defaultVal"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static T CastTo <T>(this string str, CastingContext context, T defaultVal = default) where T : class =>
 XConv.To(str, defaultVal, context);
 /// <summary>
 /// Cast to
 /// </summary>
 /// <param name="fromObj"></param>
 /// <param name="context"></param>
 /// <param name="defaultVal"></param>
 /// <typeparam name="TFrom"></typeparam>
 /// <typeparam name="TTo"></typeparam>
 /// <returns></returns>
 public static TTo CastTo <TFrom, TTo>(TFrom fromObj, CastingContext context, TTo defaultVal = default) => XConv.To(fromObj, defaultVal, context);
 /// <summary>
 /// Convert from sourceType to targetType
 /// </summary>
 /// <param name="sourceType"></param>
 /// <param name="targetType"></param>
 /// <param name="source"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static object To(Type sourceType, Type targetType, object source, CastingContext context = default)
 {
     return(XConv.To(source, sourceType, targetType, context: context));
 }