Beispiel #1
0
        public ObjectQueryContext(Type type, ObjectQueryContext parentContext)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            this.parentContext = parentContext;

            foreach (var property_info in GetProperties(type, BindingFlags.DeclaredOnly))
            {
                foreach (var attribute in property_info.GetCustomAttributes(false))
                {
                    if (ProcessElementAttribute(property_info, attribute as XmlElementAttribute))
                    {
                        break;
                    }
                    else if (ProcessAttributeAttribute(property_info, attribute as XmlAttributeAttribute))
                    {
                        break;
                    }
                    else if (ProcessArrayItemAttribute(property_info, attribute as XmlArrayItemAttribute))
                    {
                        break;
                    }
                }
            }
        }
 public ObjectQueryVisitor (ObjectQueryContext context, Object @object, Action<Object> consumer)
 {
     if (context == null) {
         throw new ArgumentNullException ("context");
     } else if (@object == null) {
         throw new ArgumentNullException ("object");
     } else if (consumer == null) {
         throw new ArgumentNullException ("consumer");
     }
     this.context = context;
     this.@object = @object;
     this.consumer = consumer;
 }
 public void Element ()
 {
     var context = new ObjectQueryContext (typeof (ElementTestClass));
     Assert.IsTrue (context.PropertyExists ("Foo", new ElementTestClass ()));
     var foo = new ElementTestClass { Foo = "bar" };
     Assert.IsTrue (context.PropertyExists ("Foo", foo));
     Assert.IsFalse (context.PropertyExists ("Bar", foo));
     var equality = false;
     context.VisitProperty ("Foo", foo, value => equality = value.Equals ("bar"));
     Assert.IsTrue (equality);
     equality = false;
     context.VisitProperty ("Foo", new ElementTestClass (), value => equality = value == null);
     Assert.IsTrue (equality);
 }
Beispiel #4
0
        ObjectQueryContext GetQueryContext(Type type)
        {
            if (type == null)
            {
                return(null);
            }

            ObjectQueryContext context;

            if (!queryContexts.TryGetValue(type, out context))
            {
                context             = new ObjectQueryContext(type, GetQueryContext(type.BaseType));
                queryContexts[type] = context;
            }

            return(context);
        }
 public ObjectQueryVisitor(ObjectQueryContext context, Object @object, Action <Object> consumer)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     else if (@object == null)
     {
         throw new ArgumentNullException("object");
     }
     else if (consumer == null)
     {
         throw new ArgumentNullException("consumer");
     }
     this.context  = context;
     this.@object  = @object;
     this.consumer = consumer;
 }
        public ObjectQueryContext (Type type, ObjectQueryContext parentContext)
        {
            if (type == null) {
                throw new ArgumentNullException ("type");
            }

            this.parentContext = parentContext;

            foreach (var property_info in GetProperties (type, BindingFlags.DeclaredOnly)) {
                foreach (var attribute in property_info.GetCustomAttributes (false)) {
                    if (ProcessElementAttribute (property_info, attribute as XmlElementAttribute)) {
                        break;
                    } else if (ProcessAttributeAttribute (property_info, attribute as XmlAttributeAttribute)) {
                        break;
                    } else if (ProcessArrayItemAttribute (property_info, attribute as XmlArrayItemAttribute)) {
                        break;
                    }
                }
            }
        }
 public void NestedArrayTestClass ()
 {
     var context = new ObjectQueryContext (typeof (NestedArrayItemTestClass));
     var attributes = new[] { "foo", "bar", "bat", "baz" };
     var foos = new[] {
         new AttributeTestClass { Foo = "foo" },
         new AttributeTestClass { Foo = "bar" },
         new AttributeTestClass { Foo = "bat" },
         new AttributeTestClass { Foo = "baz" }
     };
     var test = new NestedArrayItemTestClass { Foo = foos };
     var i = 0;
     var inequality = false;
     context.VisitProperty ("Foo@Foo", test, value => inequality |= !value.Equals (attributes[i++]));
     Assert.IsFalse (inequality);
 }
 public void PrefixedElement ()
 {
     var context = new ObjectQueryContext (typeof (PrefixedElementTestClass));
     Assert.IsTrue (context.PropertyExists ("bar:foo", new PrefixedElementTestClass ()));
 }
 public void NamedElement ()
 {
     var context = new ObjectQueryContext (typeof (NamedElementTestClass));
     Assert.IsTrue (context.PropertyExists ("foo", new NamedElementTestClass ()));
 }
 public void OmitIfNullElement ()
 {
     var context = new ObjectQueryContext (typeof (OmitIfNullElementTestClass));
     Assert.IsFalse (context.PropertyExists ("Foo", new OmitIfNullElementTestClass ()));
     Assert.IsTrue (context.PropertyExists ("Foo", new OmitIfNullElementTestClass { Foo = "bar" }));
 }
 public void NamedAttribute ()
 {
     var context = new ObjectQueryContext (typeof (NamedAttributeTestClass));
     Assert.IsTrue (context.PropertyExists ("@foo", new NamedAttributeTestClass ()));
 }
 public void NullableAttribute ()
 {
     var context = new ObjectQueryContext (typeof (NullableAttributeTestClass));
     Assert.IsTrue (context.PropertyExists ("@Foo", new NullableAttributeTestClass { Foo = 42 }));
     Assert.IsFalse (context.PropertyExists ("@Foo", new NullableAttributeTestClass ()));
 }
 public void SuperclassLookup ()
 {
     var superclass_context = new ObjectQueryContext (typeof (ElementTestClass));
     var subclass_context = new ObjectQueryContext (typeof (ElementTestSubclass), superclass_context);
     var test = new ElementTestSubclass ();
     Assert.IsTrue (superclass_context.PropertyExists ("Foo", test));
     Assert.IsFalse (superclass_context.PropertyExists ("Bar", test));
     Assert.IsTrue (subclass_context.PropertyExists ("Foo", test));
     Assert.IsTrue (subclass_context.PropertyExists ("Bar", test));
     Assert.IsFalse (new ObjectQueryContext (typeof (ElementTestSubclass)).PropertyExists ("Foo", test));
 }
 public void PrefixedArrayItem ()
 {
     var context = new ObjectQueryContext (typeof (PrefixedArrayItemTestClass));
     Assert.IsTrue (context.PropertyExists ("bar:foo", new PrefixedArrayItemTestClass { Foo = new[] { "bar" } }));
 }
 public void ArrayItem ()
 {
     var context = new ObjectQueryContext (typeof (ArrayItemTestClass));
     Assert.IsFalse (context.PropertyExists ("Foo", new ArrayItemTestClass ()));
     Assert.IsFalse (context.PropertyExists ("Foo", new ArrayItemTestClass { Foo = new string[0] }));
     var foos = new[] { "foo", "bar", "bat", "baz" };
     var test = new ArrayItemTestClass { Foo = foos };
     Assert.IsTrue (context.PropertyExists ("Foo", test));
     var i = 0;
     var inequality = false;
     context.VisitProperty ("Foo", test, value => inequality |= !value.Equals (foos[i++]));
     Assert.IsFalse (inequality);
 }
        public void NestedElements ()
        {
            var context = new ObjectQueryContext (typeof (NestedTestClass));
            var test = new NestedTestClass {
                Attribute = new AttributeTestClass (),
                OmitIfNullAttribute = new OmitIfNullAttributeTestClass (),
                NamedAttribute = new NamedAttributeTestClass (),
                PrefixedAttribute = new PrefixedAttributeTestClass (),
                PrefixedNestedAttribute = new AttributeTestClass (),
                PrefixedNestedPrefixedAttribute = new PrefixedAttributeTestClass ()
            };

            Assert.IsTrue (context.PropertyExists ("Attribute", test));
            Assert.IsTrue (context.PropertyExists ("Attribute@Foo", test));
            Assert.IsTrue (context.PropertyExists ("OmitIfNullAttribute", test));
            Assert.IsFalse (context.PropertyExists ("OmitIfNullAttribute@Foo", test));
            Assert.IsTrue (context.PropertyExists ("NamedAttribute", test));
            Assert.IsTrue (context.PropertyExists ("NamedAttribute@foo", test));
            Assert.IsTrue (context.PropertyExists ("PrefixedAttribute", test));
            Assert.IsTrue (context.PropertyExists ("PrefixedAttribute@bar:foo", test));
            Assert.IsTrue (context.PropertyExists ("bar:prefixedNestedAttribute", test));
            Assert.IsTrue (context.PropertyExists ("bar:prefixedNestedAttribute@Foo", test));
            Assert.IsTrue (context.PropertyExists ("bar:prefixedNestedPrefixedAttribute", test));
            Assert.IsTrue (context.PropertyExists ("bar:prefixedNestedPrefixedAttribute@bar:foo", test));

            test.OmitIfNullAttribute.Foo = "bar";

            Assert.IsTrue (context.PropertyExists ("OmitIfNullAttribute@Foo", test));
            var equality = false;
            context.VisitProperty ("OmitIfNullAttribute@Foo", test, value => equality = value.Equals ("bar"));
            Assert.IsTrue (equality);
        }
 public void PrefixedAttribute ()
 {
     var context = new ObjectQueryContext (typeof (PrefixedAttributeTestClass));
     Assert.IsTrue (context.PropertyExists ("@bar:foo", new PrefixedAttributeTestClass ()));
 }