public void ExtractPropertyValueCorrectlyFindSourceName()
 {
     PropertyInfoValueExtractor pe;
     pe = new PropertyInfoValueExtractor(pi);
     Assert.That(pe.SourceName, Is.EqualTo("Property"));
 }
Esempio n. 2
0
        /// <summary>
        /// Starting from current type this routine scan all field and property of the type to 
        /// find if some property of type contains a validatable object, when found that object is rescanned
        /// </summary>
        /// <param name="ruleMap"></param>
        /// <param name="typeToCheck"></param>
        /// <param name="listOfLoadedTypes">Since some objects could be already loaded, we check
        /// all properties and fields of the new types of object that are inserted.</param>
        /// <param name="listOfScannedTypes">This is an inner list that check type already scanned,
        /// consider what happended if object A has three property of type B, we do not want to 
        /// scan Three times objectB and setting an excessive number of validator.</param>
        private void RecursiveSetObjectValidator(
            Type typeToCheck,
            Dictionary<Type, ValidationUnitCollection> ruleMap,
            IList<Type> listOfLoadedTypes,
            IList<Type> listOfScannedTypes)
        {
            //If this type was not in the listofloadedtype there is no need to check, probably its set or rules is already loaded
            if (!listOfLoadedTypes.Contains(typeToCheck)) return;

            listOfScannedTypes.Add(typeToCheck);

            System.Diagnostics.Debug.Assert(ruleMap.ContainsKey(typeToCheck), "We are scanning an object that where not scanned for rules");
            //now we should check for each property and field, we need to find
            foreach (FieldInfo fi in typeToCheck.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldValidateWithObjectValidator(ruleMap, fi.FieldType))
                {
                    IValueExtractor extractor = new FieldInfoValueExtractor(fi);
                    ruleMap[typeToCheck].Add(
                        ValidationUnit.CreateObjectValidationUnit(
                            extractor, fi.Name, ruleMap));
                }
                Type type = GetTypeFromInnerMember(fi.FieldType);
                if (!listOfScannedTypes.Contains(type))
                    RecursiveSetObjectValidator(type, ruleMap, listOfLoadedTypes, listOfScannedTypes);
            }

            foreach (PropertyInfo pi in typeToCheck.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldValidateWithObjectValidator(ruleMap, pi.PropertyType))
                {
                    IValueExtractor extractor = new PropertyInfoValueExtractor(pi);
                    ruleMap[typeToCheck].Add(
                        ValidationUnit.CreateObjectValidationUnit(
                            extractor, pi.Name, ruleMap));
                }
                Type type = GetTypeFromInnerMember(pi.PropertyType);
                if (!listOfScannedTypes.Contains(type))
                    RecursiveSetObjectValidator(type, ruleMap, listOfLoadedTypes, listOfScannedTypes);
            }
        }
 public void ExtractPropertyValue()
 {
     PropertyInfoValueExtractor pe;
     pe = new PropertyInfoValueExtractor(pi);
     var test = new ClassTest1();
     test.Property = "StringTest";
     Assert.AreEqual("StringTest", pe.ExtractValue(test), "FieldExtractor Does not extract property values");
 }