Exemple #1
0
        private void SetResourceAccessorByPropertyLookup()
        {
            if (_errorMessageResourceType != null && !string.IsNullOrEmpty(_errorMessageResourceName))
            {
                var property = _errorMessageResourceType
                               .GetTypeInfo().GetDeclaredProperty(_errorMessageResourceName);
                if (property != null && !ValidationAttributeStore.IsStatic(property))
                {
                    property = null;
                }

                if (property != null)
                {
                    var propertyGetter = property.GetMethod;

                    // We only support internal and public properties
                    if (propertyGetter == null || (!propertyGetter.IsAssembly && !propertyGetter.IsPublic))
                    {
                        // Set the property to null so the exception is thrown as if the property wasn't found
                        property = null;
                    }
                }

                if (property == null)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.ValidationAttribute_ResourceTypeDoesNotHaveProperty,
                                  _errorMessageResourceType.FullName,
                                  _errorMessageResourceName));
                }

                if (property.PropertyType != typeof(string))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.ValidationAttribute_ResourcePropertyNotStringType,
                                  property.Name,
                                  _errorMessageResourceType.FullName));
                }

                _errorMessageResourceAccessor = delegate { return((string)property.GetValue(null, null)); };
            }
            else
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  SR.ValidationAttribute_NeedBothResourceTypeAndResourceName));
            }
        }
Exemple #2
0
        /// <summary>
        ///     Retrieves the property values for the given instance.
        /// </summary>
        /// <param name="instance">Instance from which to fetch the properties.</param>
        /// <param name="validationContext">Describes the entity being validated.</param>
        /// <returns>
        ///     A set of key value pairs, where the key is a validation context for the property and the value is its current
        ///     value.
        /// </returns>
        /// <remarks>Ignores indexed properties.</remarks>
        private static ICollection <KeyValuePair <ValidationContext, object> > GetPropertyValues(object instance,
                                                                                                 ValidationContext validationContext)
        {
            var properties = instance.GetType().GetRuntimeProperties()
                             .Where(p => ValidationAttributeStore.IsPublic(p) && !p.GetIndexParameters().Any());
            var items = new List <KeyValuePair <ValidationContext, object> >(properties.Count());

            foreach (var property in properties)
            {
                var context = CreateValidationContext(instance, validationContext);
                context.MemberName = property.Name;

                if (_store.GetPropertyValidationAttributes(context).Any())
                {
                    items.Add(new KeyValuePair <ValidationContext, object>(context, property.GetValue(instance, null)));
                }
            }

            return(items);
        }
        /// <summary>
        ///     Looks up the display name using the DisplayAttribute attached to the respective type or property.
        /// </summary>
        /// <returns>A display-friendly name of the member represented by the <see cref="MemberName" />.</returns>
        private string GetDisplayName()
        {
            string displayName                        = null;
            ValidationAttributeStore store            = ValidationAttributeStore.Instance;
            DisplayAttribute         displayAttribute = null;

            if (string.IsNullOrEmpty(MemberName))
            {
                displayAttribute = store.GetTypeDisplayAttribute(this);
            }
            else if (store.IsPropertyContext(this))
            {
                displayAttribute = store.GetPropertyDisplayAttribute(this);
            }

            if (displayAttribute != null)
            {
                displayName = displayAttribute.GetName();
            }

            return(displayName ?? MemberName);
        }