예제 #1
0
 private IEnumerable <BaseValidationRuleInfoAttribute> CalculateObject(object instance, List <string> properties, int?currentTaskId)
 {
     foreach (System.Reflection.PropertyInfo property in instance.GetType().GetListOfProperties())
     {
         if (properties.Contains(property.Name))
         {
             continue;
         }
         object currentValue = property.GetValue(instance, null);
         foreach (BaseValidationRuleInfoAttribute validation in GetPropertyBaseValidationRuleInfoAttributes(instance.GetType(), property, instance, currentValue, currentTaskId))
         {
             if (validation.TaskType == ValidationRuleInfoTaskType.Error)
             {
                 if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                 {
                     validation.PropertyInfo = property;
                     validation.Object       = instance;
                     validation.CurrentValue = currentValue;
                     yield return(validation);
                 }
             }
             else if (validation.TaskType == ValidationRuleInfoTaskType.ChangeValue)
             {
                 if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                 {
                     object changedValue = BaseValidationRuleInfoAttribute.GetChangedValue(validation);
                     System.Reflection.PropertyInfo findProperty = instance.GetType().GetPropertyInfo(property.Name);
                     findProperty.SetValue(instance, changedValue, null);
                 }
             }
             else
             {
                 throw new NotSupportedException();
             }
         }
     }
 }
예제 #2
0
        /// <summary>
        /// calculate all validations
        /// </summary>
        /// <returns>return list of validation errors</returns>
        public IEnumerable <BaseValidationRuleInfoAttribute> CalculateValidationsOfTask(Action <string, object> changeParameterValueAction, Action <BaseValidationRuleInfoAttribute> fillValidationParameterAction)
        {
            if (!Task.CurrentId.HasValue)
            {
                throw new Exception("cannot calculate rules without any task!");
            }
            int currentId = Task.CurrentId.GetValueOrDefault();

            if (CurrentValidationRules.TryGetValue(currentId, out List <BaseValidationRuleInfoAttribute> validationRuleInfoAttributes))
            {
                foreach (BaseValidationRuleInfoAttribute validation in validationRuleInfoAttributes)
                {
                    fillValidationParameterAction(validation);
                    if (validation.TaskType == ValidationRuleInfoTaskType.Error)
                    {
                        if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                        {
                            yield return(validation);
                        }
                    }
                    else if (validation.TaskType == ValidationRuleInfoTaskType.ChangeValue)
                    {
                        if (!BaseValidationRuleInfoAttribute.CheckIsValidate(validation))
                        {
                            object changedValue = BaseValidationRuleInfoAttribute.GetChangedValue(validation);
                            if (validation.PropertyInfo != null)
                            {
                                System.Reflection.PropertyInfo findProperty = validation.Object.GetType().GetPropertyInfo(validation.PropertyInfo.Name);
                                findProperty.SetValue(validation.Object, changedValue, null);
                            }
                            else
                            {
                                changeParameterValueAction(validation.ParameterInfo.Name, changedValue);
                            }
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            if (CurrentDetectedObjects.TryGetValue(currentId, out ConcurrentDictionary <Type, ConcurrentDictionary <object, List <string> > > types))
            {
                foreach (KeyValuePair <Type, ConcurrentDictionary <object, List <string> > > tkv in types)
                {
                    Type type = tkv.Key;
                    if (types.TryGetValue(type, out ConcurrentDictionary <object, List <string> > objects))
                    {
                        foreach (KeyValuePair <object, List <string> > okv in objects)
                        {
                            if (objects.TryGetValue(okv.Key, out List <string> properties))
                            {
                                foreach (BaseValidationRuleInfoAttribute item in CalculateArrays(okv.Key, properties, currentId))
                                {
                                    yield return(item);
                                }
                            }
                        }
                    }
                }
            }
        }