private bool ValidatePropertyUsingAnnotations(string propertyName, object value, PropertyData catelPropertyData)
{
if (!ValidateUsingDataAnnotations)
{
return true;
}
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;
}