csharp 确保结束日期不早于开始日期。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 确保结束日期不早于开始日期。相关的知识,希望对你有一定的参考价值。

using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace DataAnnotations
{
    /// <summary>
    /// Provides an attribute that compares two properties of a model and
    /// ensures that the value of the end property is not prior to the value
    /// of the begin property.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class NotBeforeAttribute : ValidationAttribute
    {
        private const string NotBefore = "'{0}' can not be before '{1}'.";
        private const string UnknownProperty = "Could not find a property named {0}.";

        private string _otherProperty;
        private string _otherPropertyDisplayName;

        /// <summary>
        /// Initializes a new instance of the <see cref="NotBeforeAttribute"/> class.
        /// </summary>
        /// <param name="otherProperty">The property to compare with the current property.</param>
        public NotBeforeAttribute(string otherProperty) : base(NotBefore)
        {
            _otherProperty = otherProperty ?? throw new ArgumentNullException(nameof(otherProperty));
        }

        public override bool RequiresValidationContext => true;

        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name,
                _otherPropertyDisplayName ?? _otherProperty);
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var otherPropertyInfo = validationContext.ObjectType.GetRuntimeProperty(_otherProperty);

            if (otherPropertyInfo == null)
            {
                return new ValidationResult(string.Format(CultureInfo.CurrentCulture, UnknownProperty, _otherProperty));
            }

            object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);

            if ((dynamic)value < (dynamic)otherPropertyValue)
            {
                if (_otherPropertyDisplayName == null)
                {
                    _otherPropertyDisplayName = GetDisplayNameForProperty(validationContext.ObjectType, otherPropertyInfo);
                }
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }

            return null;
        }

        private string GetDisplayNameForProperty(Type containerType, PropertyInfo property)
        {
            var attributes = CustomAttributeExtensions.GetCustomAttributes(property, true);
            var display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
            if (display != null)
            {
                return display.GetName();
            }

            return _otherProperty;
        }
    }
}

以上是关于csharp 确保结束日期不早于开始日期。的主要内容,如果未能解决你的问题,请参考以下文章

触发检查逻辑日期更新

为 2 Datepicker 开始日期和结束日期设置验证 javascript

Angular 2表单验证开始日期<=结束日期

element禁用当前系统日期之前,为什么不生效

从触发器调用过程以验证生日

按较早日期验证两个日期时间本地字段?