示例#1
0
        /// <summary>
        /// Is
        /// </summary>
        /// <param name="s"></param>
        /// <param name="type"></param>
        /// <param name="action"></param>
        /// <param name="ignoreCase"></param>
        /// <param name="format"></param>
        /// <param name="numberStyle"></param>
        /// <param name="dateTimeStyle"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static bool Is(this string s, Type type, IgnoreCase ignoreCase = IgnoreCase.FALSE, Action <object> action = null,
                              string format = null, NumberStyles?numberStyle = null, DateTimeStyles?dateTimeStyle = null, IFormatProvider provider = null)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            __unsupportedTypeCheck(type, out var typeIsAssignableFromEncoding);

            return(TypeIs.__enumIs(s, type, action, ignoreCase) ||
                   TypeIs.__charIs(s, type, action) ||
                   TypeIs.__numericIs(s, type, action, numberStyle, provider) ||
                   TypeIs.__booleanIs(s, type, action) ||
                   TypeIs.__dateTimeIs(s, type, action, format, dateTimeStyle, provider) ||
                   TypeIs.__dateTimeOffsetIs(s, type, action, format, dateTimeStyle, provider) ||
                   TypeIs.__timeSpanIs(s, type, action, format, provider) ||
                   TypeIs.__guidIs(s, type, action, format) ||
                   TypeIs.__versionIs(s, type, action) ||
                   TypeIs.__ipAddressIs(s, type, action) ||
                   TypeIs.__encodingIs(s, action, typeIsAssignableFromEncoding));

            void __unsupportedTypeCheck(Type t, out bool flag)
            {
                flag = typeof(Encoding).IsAssignableFrom(t);
                if (!t.IsValueType && !flag && t == typeof(Version) && t == typeof(IPAddress))
                {
                    throw new ArgumentException("Unsupported type");
                }
            }
        }
        /// <summary>
        /// Determine whether the given string can be of the given type. <br />
        /// 判断给定的字符串是否能成为给定的类型。
        /// </summary>
        /// <param name="text"></param>
        /// <param name="type"></param>
        /// <param name="matchedCallback"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static bool Is(this string text, Type type, CastingContext context, Action <object> matchedCallback = null)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (Types.IsNullableType(type))
            {
                return(text is null || Is(text, TypeConv.GetNonNullableType(type), context, matchedCallback));
            }

            if (!__unsupportedTypeCheck(type,
                                        out var typeIsAssignableFromEncoding,
                                        out var typeCanBeChecking, out var checkingHandler))
            {
                return(false);
            }

            if (typeCanBeChecking)
            {
                return(__customChecking(checkingHandler));
            }

            return(TypeIs.__enumIs(text, type, matchedCallback, context.IgnoreCase)
                   .Or(() => TypeIs.__charIs(text, type, matchedCallback))
                   .Or(() => TypeIs.__numericIs(text, type, matchedCallback, context.NumberStyles, context.FormatProvider))
                   .Or(() => TypeIs.__booleanIs(text, type, matchedCallback))
                   .Or(() => TypeIs.__dateTimeIs(text, type, matchedCallback, context.Format, context.DateTimeStyles, context.FormatProvider))
                   .Or(() => TypeIs.__dateTimeOffsetIs(text, type, matchedCallback, context.Format, context.DateTimeStyles, context.FormatProvider))
                   .Or(() => TypeIs.__timeSpanIs(text, type, matchedCallback, context.Format, context.FormatProvider))
                   .Or(() => TypeIs.__guidIs(text, type, matchedCallback, context.Format))
                   .Or(() => TypeIs.__versionIs(text, type, matchedCallback))
                   .Or(() => TypeIs.__ipAddressIs(text, type, matchedCallback))
                   .Or(() => TypeIs.__encodingIs(text, matchedCallback, typeIsAssignableFromEncoding)));

            // ReSharper disable once InconsistentNaming
            bool __unsupportedTypeCheck(Type t, out bool encodingFlag, out bool checkingFlag, out Func <object, bool> checker)
            {
                encodingFlag = t == typeof(Encoding) || TypeReflections.IsTypeDerivedFrom(t, typeof(Encoding), TypeDerivedOptions.CanAbstract);
                checkingFlag = CustomConvertManager.TryGetChecker(TypeClass.StringClazz, t, out checker);
                return(t.IsValueType
                       .Or(encodingFlag)
                       .Or(checkingFlag)
                       .Or(() => t == typeof(Version))
                       .Or(() => t == typeof(IPAddress)));
            }

            // ReSharper disable once InconsistentNaming
            bool __customChecking(Func <object, bool> handler)
            {
                var result = handler?.Invoke(text) ?? false;

                result.IfTrue(matchedCallback, text);

                return(result);
            }
        }