예제 #1
0
        private IValidator GetChildValidator(IChildValidatorAdaptor adaptor)
        {
            var validatorContext         = new ValidationContext <object>(null);
            var propertyValidatorContext = new PropertyValidatorContext(validatorContext, null, null, null);

            return(((dynamic)adaptor).GetValidator(propertyValidatorContext));
        }
        internal static IValidator?GetValidatorGeneric <T>(this IChildValidatorAdaptor childValidatorAdapter)
        {
            // public class ChildValidatorAdaptor<T,TProperty>
            // public virtual IValidator GetValidator(ValidationContext<T> context, TProperty value) {
            var getValidatorMethodName = nameof(ChildValidatorAdaptor <object, object> .GetValidator);
            var getValidatorMethod     = childValidatorAdapter.GetType().GetMethod(getValidatorMethodName);

            if (getValidatorMethod != null)
            {
                // Fake context. We have not got real context because no validation yet.
                var    fakeContext = new ValidationContext <T>(default);
        private static IValidator?GetValidator(IChildValidatorAdaptor adaptor, PropertyValidatorContext context)
        {
            var cache       = typedChildValidatorAdaptorCache.Value;
            var adaptorType = adaptor.GetType( );

            if (!cache.TryGetValue(adaptorType, out var getValidator))
            {
                cache [adaptorType] = getValidator = adaptorType.GetRuntimeMethod(nameof(ChildValidatorAdaptor <object, object> .GetValidator),
                                                                                  new [] { typeof(PropertyValidatorContext) });
            }

            return((IValidator?)getValidator?.Invoke(adaptor, new [] { context }));
        }
        private IValidator GetValidatorFromChildValidatorAdapter(IChildValidatorAdaptor childValidatorAdapter)
        {
            // Fake context. We have not got real context because no validation yet.
            var fakeContext = new PropertyValidatorContext(new ValidationContext <object>(null), null, string.Empty);

            // Try to validator with reflection.
            var childValidatorAdapterType = childValidatorAdapter.GetType();
            var getValidatorMethod        = childValidatorAdapterType.GetMethod(nameof(ChildValidatorAdaptor <object, object> .GetValidator));

            if (getValidatorMethod != null)
            {
                var validator = (IValidator)getValidatorMethod.Invoke(childValidatorAdapter, new[] { fakeContext });
                return(validator);
            }

            return(null);
        }
        internal static IValidator?GetValidatorFromChildValidatorAdapter(this IChildValidatorAdaptor childValidatorAdapter)
        {
            // Try to validator with reflection.
            var childValidatorAdapterType = childValidatorAdapter.GetType();
            var genericTypeArguments      = childValidatorAdapterType.GenericTypeArguments;

            if (genericTypeArguments.Length != 2)
            {
                return(null);
            }

            var getValidatorGeneric = typeof(FluentValidationSchemaBuilder)
                                      .GetMethod(nameof(GetValidatorGeneric), BindingFlags.Static | BindingFlags.NonPublic)
                                      ?.MakeGenericMethod(genericTypeArguments[0]);

            if (getValidatorGeneric != null)
            {
                var validator = (IValidator)getValidatorGeneric.Invoke(null, new [] { childValidatorAdapter });
                return(validator);
            }

            return(null);
        }