コード例 #1
0
        /// <summary>
        /// Valida um objeto
        /// </summary>
        /// <param name="targetObject">Objeto a ser validado</param>
        /// <param name="validationResult">Objeto aonde o resultado da validação será anexado</param>
        public virtual void Validate(IStateControl targetObject, ref ValidationResult validationResult)
        {
            targetObject.Require("targetObject").NotNull();
            if (targetObject is IDisposableState && ((IDisposableState)targetObject).IsDisposed)
            {
                return;
            }
            if (validationResult == null)
            {
                validationResult = new ValidationResult();
            }
            var properties = targetObject.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(f => f.CanRead && !f.GetCustomAttributes(false).Any(x => x is IgnoreValidationAttribute));

            foreach (PropertyInfo property in properties)
            {
                if (property.Name != "IsLocked")
                {
                    var state = targetObject.InstanceState[property.Name];
                    if (state != null && state.Validation != null)
                    {
                        var propertyValue = property.GetValue(targetObject, null);
                        ApplyValidation(state.Validation, property.Name, state.Label, targetObject, propertyValue, ref validationResult);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Valida uma propriedade de um objeto
        /// </summary>
        /// <param name="propertyValue">Valor da propriedade</param>
        /// <param name="propertyName">Nome da propriedade</param>
        /// <param name="targetObject">Objeto alvo</param>
        /// <param name="validationResult">Objeto aonde o resultado da validação será anexado</param>
        public virtual void ValidateProperty(IStateControl targetObject, string propertyName, object propertyValue, ref ValidationResult validationResult)
        {
            targetObject.Require("targetObject").NotNull();
            propertyName.Require("propertyName").NotNull().NotEmpty();
            var statebleItem = targetObject.InstanceState[propertyName];

            if (statebleItem != null && statebleItem.Validation != null)
            {
                ApplyValidation(statebleItem.Validation, propertyName, statebleItem.Label, targetObject, propertyValue, ref validationResult);
            }
        }