Hishop.Components.Validation/Validators/DateTimeRangeValidatorAttribute.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Hishop.Components.Validation.Validators
{
    using Hishop.Components.Validation;
    using Hishop.Components.Validation.Properties;
    using System;
    using System.Globalization;

    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
    public sealed class DateTimeRangeValidatorAttribute : ValueValidatorAttribute
    {
        private DateTime lowerBound;
        private RangeBoundaryType lowerBoundType;
        private DateTime upperBound;
        private RangeBoundaryType upperBoundType;

        public DateTimeRangeValidatorAttribute(DateTime upperBound) : this(new DateTime(), RangeBoundaryType.Ignore, upperBound, RangeBoundaryType.Inclusive)
        {
        }

        public DateTimeRangeValidatorAttribute(string upperBound) : this(ConvertToISO8601Date(upperBound))
        {
        }

        public DateTimeRangeValidatorAttribute(DateTime lowerBound, DateTime upperBound) : this(lowerBound, RangeBoundaryType.Inclusive, upperBound, RangeBoundaryType.Inclusive)
        {
        }

        public DateTimeRangeValidatorAttribute(string lowerBound, string upperBound) : this(ConvertToISO8601Date(lowerBound), ConvertToISO8601Date(upperBound))
        {
        }

        public DateTimeRangeValidatorAttribute(DateTime lowerBound, RangeBoundaryType lowerBoundType, DateTime upperBound, RangeBoundaryType upperBoundType)
        {
            this.lowerBound = lowerBound;
            this.lowerBoundType = lowerBoundType;
            this.upperBound = upperBound;
            this.upperBoundType = upperBoundType;
        }

        public DateTimeRangeValidatorAttribute(string lowerBound, RangeBoundaryType lowerBoundType, string upperBound, RangeBoundaryType upperBoundType) : this(ConvertToISO8601Date(lowerBound), lowerBoundType, ConvertToISO8601Date(upperBound), upperBoundType)
        {
        }

        private static DateTime ConvertToISO8601Date(string iso8601DateString)
        {
            DateTime time;
            if (string.IsNullOrEmpty(iso8601DateString))
            {
                return new DateTime();
            }
            try
            {
                time = DateTime.ParseExact(iso8601DateString, "s", CultureInfo.InvariantCulture);
            }
            catch (FormatException exception)
            {
                throw new ArgumentException(Resources.ExceptionInvalidDate, "dateString", exception);
            }
            return time;
        }
后面还有 8 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。