public static ICollectionValidatorRuleBuilder <T, TCollectionElement> SetCollectionValidator <T, TCollectionElement>(this IRuleBuilder <T, IEnumerable <TCollectionElement> > ruleBuilder, IValidator <TCollectionElement> validator)
        {
            IValidator <T> parentValidator = null;

            // Delegate to the RuleForEach implementation.
            var innerValidator   = new WrapperHack <TCollectionElement>(validator.GetType());
            var innerRuleBuilder = (RuleBuilder <IEnumerable <TCollectionElement>, TCollectionElement>)innerValidator.RuleForEach(x => x);

            innerRuleBuilder.SetValidator(validator);

            //Copy across any rulesets
            ((IRuleBuilderOptions <T, IEnumerable <TCollectionElement> >)ruleBuilder).Configure(cfg => {
                innerRuleBuilder.Rule.RuleSets = cfg.RuleSets;
            });

            ruleBuilder.SetValidator(new ChildValidatorAdaptor(innerValidator, validator.GetType())
            {
                PassThroughParentContext = true
            });

            if (ruleBuilder is IExposesParentValidator <T> exposesParentValidator)
            {
                parentValidator = exposesParentValidator.ParentValidator;
            }

            return(new CollectionValidatorRuleBuilder <T, TCollectionElement>(ruleBuilder, innerRuleBuilder, parentValidator));
        }
        public static ICollectionValidatorRuleBuilder <T, TCollectionElement> SetCollectionValidator <T, TCollectionElement, TValidator>(this IRuleBuilder <T, IEnumerable <TCollectionElement> > ruleBuilder, Func <T, TValidator> validator)
            where TValidator : IValidator <TCollectionElement>
        {
            IValidator <T> parentValidator = null;

            // Delegate to the RuleForEach implementation.
            var innerValidator   = new WrapperHack <TCollectionElement>(typeof(TValidator));
            var innerRuleBuilder = ((RuleBuilder <IEnumerable <TCollectionElement>, TCollectionElement>)innerValidator.RuleForEach(x => x));

            innerRuleBuilder.SetValidator(context => {
                while (context.ParentContext != null)
                {
                    context = context.ParentContext;
                }

                var model = (T)context.InstanceToValidate;
                return(validator(model));
            });

            //Copy across any rulesets
            ((IRuleBuilderOptions <T, IEnumerable <TCollectionElement> >)ruleBuilder).Configure(cfg => {
                innerRuleBuilder.Rule.RuleSets = cfg.RuleSets;
            });

            ruleBuilder.SetValidator(new ChildValidatorAdaptor(innerValidator, typeof(TValidator))
            {
                PassThroughParentContext = true
            });

            if (ruleBuilder is IExposesParentValidator <T> exposesParentValidator)
            {
                parentValidator = exposesParentValidator.ParentValidator;
            }

            return(new CollectionValidatorRuleBuilder <T, TCollectionElement>(ruleBuilder, innerRuleBuilder, parentValidator));
        }