Catel.FluentValidatorProvider.GetValidator C# (CSharp) Method

GetValidator() protected method

Gets a validator for the specified target type.
This method only searches for fluent validators on the assembly which the targetType belongs to, and creates adapters that allow fluent validator talks with catel validation approach.
protected GetValidator ( Type targetType ) : Data.IValidator
targetType System.Type /// The target type. ///
return Data.IValidator
        protected override IValidator GetValidator(Type targetType)
        {
            IValidator validator = null;

            // NOTE: Patch for performance issue the validator of a viewmodel must be in the same assembly of the view model.

            var assembly = targetType.GetAssemblyEx();
            var exportedTypes = assembly.GetExportedTypesEx();

            var validatorTypes = new List<Type>();
            foreach (var exportedType in exportedTypes)
            {
                if (typeof(FluentValidation.IValidator).IsAssignableFromEx(exportedType))
                {
                    var currentType = exportedType;
                    bool found = false;
                    while (!found && currentType != typeof(object))
                    {
                        if (currentType != null)
                        {
                            var typeInfo = currentType.GetTypeInfo();
                            var genericArguments = currentType.GetGenericArgumentsEx();

                            found = typeInfo.IsGenericType && genericArguments.FirstOrDefault(type => type.IsAssignableFromEx(targetType)) != null;
                            if (!found)
                            {
                                currentType = typeInfo.BaseType;
                            }
                        }
                    }

                    if (found)
                    {
                        validatorTypes.Add(exportedType);
                    }
                }
            }

            if (validatorTypes.Count > 0)
            {
                validator = FluentValidatorToCatelValidatorAdapter.From(validatorTypes);
            }

            return validator;
        }