/// <summary> /// Validates the property using data annotations. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="value">The value to validate.</param> /// <param name="catelPropertyData">The catel property data. Can be <c>null</c> for non-Catel properties.</param> /// <returns><c>true</c> if no errors using data annotations are found; otherwise <c>false</c>.</returns> private bool ValidatePropertyUsingAnnotations(string propertyName, object value, PropertyData catelPropertyData) { if (SuspendValidation) { _propertiesNotCheckedDuringDisabledValidation.Add(propertyName); return true; } #if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35 var type = GetType(); try { if (!_propertyValuesIgnoredOrFailedForValidation[type].Contains(propertyName)) { if (catelPropertyData != null) { var propertyInfo = catelPropertyData.GetPropertyInfo(type); if (propertyInfo == null || !propertyInfo.HasPublicGetter) { _propertyValuesIgnoredOrFailedForValidation[type].Add(propertyName); return false; } } else { #if NET if (type.GetPropertyEx(propertyName) == null) { Log.Debug("Property '{0}' cannot be found via reflection, ignoring this property for type '{1}'", propertyName, type.FullName); _propertyValuesIgnoredOrFailedForValidation[type].Add(propertyName); return false; } #else // Checking via reflection is faster than catching the exception if (!Reflection.PropertyHelper.IsPublicProperty(this, propertyName)) { Log.Debug("Property '{0}' is not a public property, cannot validate non-public properties in silverlight", propertyName); _propertyValuesIgnoredOrFailedForValidation[type].Add(propertyName); return false; } #endif } if (!_dataAnnotationsValidationContext.ContainsKey(propertyName)) { _dataAnnotationsValidationContext[propertyName] = new System.ComponentModel.DataAnnotations.ValidationContext(this, null, null) { MemberName = propertyName }; } System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, _dataAnnotationsValidationContext[propertyName]); // If succeeded, clear any previous error if (_dataAnnotationValidationResults.ContainsKey(propertyName)) { _dataAnnotationValidationResults[propertyName] = null; } } } catch (System.ComponentModel.DataAnnotations.ValidationException validationException) { _dataAnnotationValidationResults[propertyName] = validationException.Message; return false; } catch (Exception ex) { _propertyValuesIgnoredOrFailedForValidation[type].Add(propertyName); Log.Warning(ex, "Failed to validate property '{0}' via Validator (property does not exists?)", propertyName); } #endif return true; }