/// <summary>
 /// Cast <see cref="object"/> to given type.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="targetType"></param>
 /// <param name="defaultVal"></param>
 /// <returns></returns>
 public static object CastTo(this object obj, Type targetType, object defaultVal)
 {
     CastTypeHelper.Guard(targetType, nameof(targetType));
     return(obj is null
         ? defaultVal
         : XConv.To(obj, obj.GetType(), targetType, defaultVal));
 }
 /// <summary>
 /// Cast <see cref="object"/> to given type.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="targetType"></param>
 /// <returns></returns>
 public static object CastTo(this object obj, Type targetType)
 {
     CastTypeHelper.Guard(targetType, nameof(targetType));
     return(obj is null
         ? default
         : XConv.To(obj, obj.GetType(), targetType, TypeDeterminer.GetDefaultValue(targetType)));
 }
        /// <summary>
        /// Cast to nullable
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <param name="contextAct"></param>
        /// <returns></returns>
        public static object CastToNullable(this string str, Type type, Action <CastingContext> contextAct)
        {
            CastTypeHelper.Guard(type, nameof(type));
            object result = default;

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

            return(str.Is(targetType, v => result = v)
                ? result
                : XConv.To(str, TypeClass.StringClazz, targetType, defaultVal));
        }
        /// <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 <see cref="object"/> to the given type of <see cref="Enum"/>.
 /// </summary>
 /// <param name="fromObj"></param>
 /// <param name="enumTye"></param>
 /// <param name="validation"></param>
 /// <returns></returns>
 public static object CastTo(object fromObj, Type enumTye, EnumsNET.EnumValidation validation)
 {
     CastTypeHelper.Guard(enumTye, nameof(enumTye));
     return(EnumsNET.Enums.ToObject(enumTye, fromObj, validation));
 }
 /// <summary>
 /// Cast <see cref="object"/> to given type.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="sourceType"></param>
 /// <param name="targetType"></param>
 /// <param name="defaultVal"></param>
 /// <returns></returns>
 public static object CastTo(this object obj, Type sourceType, Type targetType, object defaultVal)
 {
     CastTypeHelper.Guard(sourceType, nameof(sourceType));
     CastTypeHelper.Guard(targetType, nameof(targetType));
     return(XConv.To(obj, sourceType, targetType, defaultVal));
 }