Exemple #1
0
        static void Main(string[] args)
        {
            AssetEditable asset = GenerateSampleAsset();

            var enumerator = new ExpressionEnumerator(typeof(AssetEditable)).ToList();
            var evaluator = new ExpressionEvaluator();

            Console.WriteLine("Type of {0} : ", typeof(AssetEditable).AssemblyQualifiedName);
            foreach (KeyValuePair<string,string> k in enumerator)
            {
                Console.WriteLine("Property name : {0}", k.Key);
                Console.WriteLine("Value : {0}", evaluator.GetValue(asset, k.Key));
                Console.WriteLine();
            }
            Console.ReadLine();
        }
 private static bool ProcessConstraint(object item, ValidationConstraint constraint)
 {
     var argumentType = item.GetType();
     object mainArgument = null;
     object secondArgument = null;
     if (constraint.ObjectType == argumentType.AssemblyQualifiedName)
     {
         var evaluator = new ExpressionEvaluator();
         var propertyValue = evaluator.GetValue(item, constraint.Property);
         if (constraint.MainArgument != null) mainArgument = GetValue(constraint.MainArgument, propertyValue.GetType());
         if (constraint.SecondaryArgument != null) secondArgument = GetValue(constraint.SecondaryArgument, propertyValue.GetType());
         switch (constraint.ConstraintType)
         {
             case trkValidationConstraintType.Null:
                 if (propertyValue is string)
                     return string.IsNullOrEmpty(propertyValue.ToString());
                 return (propertyValue == null);
             case trkValidationConstraintType.NotNull:
                 if (propertyValue is string)
                     return !string.IsNullOrEmpty((string)propertyValue.ToString());
                 return (propertyValue != null);
             case trkValidationConstraintType.Greater:
                 if (mainArgument == null) return false;
                 return IsGreater(mainArgument, propertyValue);
             case trkValidationConstraintType.Lower:
                 if (mainArgument == null) return false;
                 return IsGreater(propertyValue, mainArgument);
             case trkValidationConstraintType.Equal:
                 if (mainArgument == null) return false;
                 return AreEqual(propertyValue, mainArgument);
             case trkValidationConstraintType.Between:
                 if (mainArgument == null) return false;
                 if (secondArgument == null) return false;
                 return (IsGreater(mainArgument, propertyValue) && IsGreater(propertyValue, secondArgument));
             case trkValidationConstraintType.Different:
                 return !AreEqual(propertyValue, mainArgument);
             case trkValidationConstraintType.False:
                 return !((bool)propertyValue);
             case trkValidationConstraintType.True:
                 return ((bool)propertyValue);
             default:
                 throw new ArgumentOutOfRangeException();
         }
     }
     return false;
 }