예제 #1
0
        public List <ClientValidationRule> GetClientValidationRules(Type type, string propertyName)
        {
            Verify.ArgumentNotNull(type, "type");
            Verify.ArgumentNotNullOrEmpty(propertyName, "propertyName");

            PropertyInfo propertyInfo = type.GetProperty(propertyName);

            Verify.IsNotNull(propertyInfo, "The property named '{0}' not found on the type '{1}'", propertyName, type);

            List <ValidatorAttribute> attributes = propertyInfo.GetCustomAttributesRecursively <ValidatorAttribute>().ToList();

            var rules = new List <ClientValidationRule>();

            foreach (ValidatorAttribute attribute in attributes)
            {
                string translatorName = ClientValidationRuleTranslatorRegistry.GetTranslatorName(attribute.GetType());

                if (translatorName != null)
                {
                    ClientValidationRule rule = ClientValidationRuleTranslatorPluginFacade.Translate(translatorName, attribute);

                    rules.Add(rule);
                }
            }

            return(rules);
        }
            public static void Initialize(Resources resources)
            {
                resources.AttributeTypeToTranslatorName = new Dictionary <Type, string>();

                if (ConfigurationServices.ConfigurationSource == null)
                {
                    throw new ConfigurationErrorsException(string.Format("No configuration source specified"));
                }

                ClientValidationRuleTranslatorSettings settings = ConfigurationServices.ConfigurationSource.GetSection(ClientValidationRuleTranslatorSettings.SectionName) as ClientValidationRuleTranslatorSettings;

                if (settings == null)
                {
                    throw new ConfigurationErrorsException(string.Format("Failed to load the configuration section '{0}' from the configuration.", ClientValidationRuleTranslatorSettings.SectionName));
                }

                foreach (ClientValidationRuleTranslatorData data in settings.ClientValidationRuleTranslatorPlugins)
                {
                    IEnumerable <Type> types = ClientValidationRuleTranslatorPluginFacade.GetSupportedAttributeTypes(data.Name);

                    foreach (Type type in types)
                    {
                        if (resources.AttributeTypeToTranslatorName.ContainsKey(type))
                        {
                            throw new InvalidOperationException(string.Format("The attribute type '{0}' is already handle by a nother translator", type));
                        }
                        if (typeof(ValidatorAttribute).IsAssignableFrom(type) == false)
                        {
                            throw new InvalidOperationException(string.Format("The type '{0}' is not an {1}", type, typeof(ValidatorAttribute)));
                        }

                        resources.AttributeTypeToTranslatorName.Add(type, data.Name);
                    }
                }
            }