Example #1
0
        /// <summary>
        /// Gets or creates generic validation context from non-generic validation context.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        public static ValidationContext <T> GetFromNoNGenericContext(ValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Already of the correct type.
            if (context is ValidationContext <T> c)
            {
                return(c);
            }

            // Parameters match
            if (context.InstanceToValidate is T)
            {
                return(context.ToGeneric <T>());
            }

            throw new NotSupportedException("context.InstanceToValidate is not of type " + typeof(T).FullName);
        }
        /// <summary>
        /// Gets or creates generic validation context from non-generic validation context.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        public static ValidationContext <T> GetFromNonGenericContext(ValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Already of the correct type.
            if (context is ValidationContext <T> c)
            {
                return(c);
            }

            // Parameters match
            if (context.InstanceToValidate is T)
            {
                return(context.ToGeneric <T>());
            }

            if (context.InstanceToValidate == null)
            {
                return(new ValidationContext <T>(default, context.PropertyChain, context.Selector)