Mercurial > pub > bltoolkit
diff Source/Validation/ValidatorBaseAttribute.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Source/Validation/ValidatorBaseAttribute.cs Thu Mar 27 21:46:09 2014 +0400 @@ -0,0 +1,69 @@ +using System; +using System.Reflection; +using System.ComponentModel; + +namespace BLToolkit.Validation +{ + public abstract class ValidatorBaseAttribute : Attribute + { + protected ValidatorBaseAttribute() + { + } + + protected ValidatorBaseAttribute(string errorMessage) + { + _errorMessage = errorMessage; + } + + private string _errorMessage; + public virtual string ErrorMessage + { + get { return _errorMessage; } + set { _errorMessage = value; } + } + + public abstract bool IsValid(ValidationContext context); + + public virtual string GetErrorMessage(ValidationContext context) + { + return string.Format(ErrorMessage, GetPropertyFriendlyName(context)); + } + + protected virtual string GetPropertyFriendlyName(ValidationContext context) + { + MemberInfo mi = context.MemberInfo; + string className = mi.DeclaringType.Name; + string fieldName = mi.Name; + + // Get class name. + // + object[] attrs = mi.DeclaringType.GetCustomAttributes(typeof(FriendlyNameAttribute), true); + + if (attrs.Length > 0) + className = ((FriendlyNameAttribute)attrs[0]).Name; + else + { + attrs = mi.DeclaringType.GetCustomAttributes(typeof(DisplayNameAttribute), true); + + if (attrs.Length > 0) + className = ((DisplayNameAttribute)attrs[0]).DisplayName; + } + + // Get field name. + // + attrs = mi.GetCustomAttributes(typeof(FriendlyNameAttribute), true); + + if (attrs.Length > 0) + fieldName = ((FriendlyNameAttribute)attrs[0]).Name; + else + { + attrs = mi.GetCustomAttributes(typeof(DisplayNameAttribute), true); + + if (attrs.Length > 0) + fieldName = ((DisplayNameAttribute)attrs[0]).DisplayName; + } + + return string.IsNullOrEmpty(className)? fieldName: className + "." + fieldName; + } + } +}