예제 #1
0
        public void ShouldNotExplodeWithNonExistingKey()
        {
            string xmlstring = @"
                <add routeName=""default"" path=""{controller}/{action}"">
                    <defaultValues controller=""Home"" action=""Index""/>
                </add>";
            dynamic route = new DynamicXmlElement(xmlstring);

            Assert.DoesNotThrow(
                () =>
                {
                    var notExistingNode = route.constraints;
                });
        }
예제 #2
0
        public void ShouldLoadDynamicFromXmlString()
        {
            string xmlString = @"<Contacts><User name=""user1""/><User name=""user2""/></Contacts>";
            dynamic xml = new DynamicXmlElement(xmlString);

            dynamic users = xml.User;

            Assert.IsNotNull(users);
            Assert.IsTrue(users.Count == 2);
            foreach (dynamic user in users)
            {
                Assert.IsNotNull(user.name);
            }
            string user1 = users[0].name;
            Assert.AreEqual(user1, "user1");

            string user2 = users[1].name;
            Assert.AreEqual(user2, "user2");
        }
예제 #3
0
        public void ShouldGetValuesFromDynamicXmlRoute()
        {
            string xmlstring = @"
                <add routeName=""default"" path=""{controller}/{action}"">
                    <defaultValues controller=""Home"" action=""Index""/>
                </add>";
            dynamic route = new DynamicXmlElement(xmlstring);

            //Assert.AreEqual(route.Name, "add");
            Assert.AreEqual(route.routeName, "default");
            Assert.AreEqual(route.path, "{controller}/{action}");

            dynamic defaults = route.defaultValues;

            Assert.AreEqual(defaults.controller,"Home");
            var anon = new { controller = "Home", action = "Index" };
            Assert.AreEqual(defaults.controller, anon.controller);

            Dictionary<string, object> dico = new Dictionary<string, object>();
            dico.Add("controller","Home");
            dico.Add("action", "About");
        }
예제 #4
0
        private bool TryGetXmlMember(string name, out object result)
        {
            //child name should map to one or many elements, so let's use a list by default
            IEnumerable <XElement> nodes = _InternalNode.Elements(name);


            //if something found it may be a _InternalNode or a list of child nodes
            if (nodes != null && nodes.Count() > 0)
            {
                if (nodes.Count() == 1)
                {
                    XElement current = nodes.First();
                    if (current.HasElements)
                    {
                        List <string> list = new List <string>(nodes.Elements().Select(x => x.Value));
                        result = list;
                    }
                    else
                    {
                        result = new DynamicXmlElement(nodes.First());
                    }
                }
                else
                {
                    List <DynamicXmlElement> list = new List <DynamicXmlElement>(nodes.Select(x => new DynamicXmlElement(x)));
                    result = list;
                }
                return(true);
            }
            //else it should be an attribute name
            else
            {
                IEnumerable <XAttribute> getAttrib = _InternalNode.Attributes(name);
                //finally try to see if the call is made to a member of XElement
                if (getAttrib != null && getAttrib.Count() > 0)
                {
                    //if it matches at least one attribute, return directly an XAttribue, as you don't want to
                    //map it as a dynamic node (after all, we can view it as a terminal leaf in a dynamic tree)
                    if (getAttrib.Count() > 1)
                    {
                        result = getAttrib;
                    }
                    else
                    {
                        result = getAttrib.First().Value;
                    }
                    return(true);
                }
                else
                {
                    Type         xet     = typeof(XElement);
                    MemberInfo[] members = xet.GetMember(name);
                    if (members != null && members.Count() > 0)
                    {
                        MemberInfo one = members[0];
                        if (one is PropertyInfo)
                        {
                            var property = (PropertyInfo)one;
                            result = property.GetValue(_InternalNode, null);
                            return(true);
                        }
                        else if (one is MethodInfo)
                        {
                            var method = (MethodInfo)one;
                            result = method.Invoke(_InternalNode, null);
                            return(true);
                        }
                        result = new DynamicXmlElement(new XElement(name));
                        return(true);
                    }
                    else
                    {
                        result = new DynamicXmlElement(new XElement(name));
                        return(true);
                    }
                }
            }
        }
예제 #5
0
        public void ShouldMapADynamicXmlObjectToClassWithNestedElements()
        {
            string xmlstring = @"
                <Contact Name=""Rui"" Id=""111"">
                    <phone>1234567890</phone>
                    <mail>[email protected]</mail>
                    <cities>
                        <city>Paris</city>
                        <city>Lisbon</city>
                    </cities>
                </Contact>
                ";
            dynamic contactDynamic = new DynamicXmlElement(xmlstring);

            Contact expected = new Contact();
            expected.Name = "Rui";
            expected.Id = 111;
            expected.Mail = "*****@*****.**";
            expected.Phone = "1234567890";
            expected.Cities.Add("Paris");
            expected.Cities.Add("Lisbon");
            Contact actual = ValueFactory.TryGet<Contact>(contactDynamic);
            Assert.AreEqual(actual, expected);
        }
예제 #6
0
        public void ShouldMapADynamicXmlObjectToARealImplementation()
        {
            string xmlstring = @"
                <Contact Name=""Rui"" Id=""111"" />";
            dynamic contactDynamic = new DynamicXmlElement(xmlstring);

            Contact expected = new Contact();
            expected.Name = "Rui";
            expected.Id = 111;

            Contact actual = ValueFactory.TryGet<Contact>(contactDynamic);
            Assert.AreEqual(actual, expected);
        }
예제 #7
0
파일: DynamicXml.cs 프로젝트: rhwy/ConfArt
        private bool TryGetXmlMember(string name, out object result)
        {
            //child name should map to one or many elements, so let's use a list by default
            IEnumerable<XElement> nodes = _InternalNode.Elements(name);

            //if something found it may be a _InternalNode or a list of child nodes
            if (nodes != null && nodes.Count() > 0)
            {
                if (nodes.Count() == 1)
                {
                    XElement current = nodes.First();
                    if (current.HasElements)
                    {
                        List<string> list = new List<string>(nodes.Elements().Select(x => x.Value));
                        result = list;
                    }
                    else
                    {
                        result = new DynamicXmlElement(nodes.First());
                    }

                }
                else
                {
                    List<DynamicXmlElement> list = new List<DynamicXmlElement>(nodes.Select(x => new DynamicXmlElement(x)));
                    result = list;
                }
                return true;
            }
            //else it should be an attribute name
            else
            {

                IEnumerable<XAttribute> getAttrib = _InternalNode.Attributes(name);
                //finally try to see if the call is made to a member of XElement
                if (getAttrib != null && getAttrib.Count() > 0)
                {
                    //if it matches at least one attribute, return directly an XAttribue, as you don't want to
                    //map it as a dynamic node (after all, we can view it as a terminal leaf in a dynamic tree)
                    if (getAttrib.Count() > 1)
                    {
                        result = getAttrib;
                    }
                    else
                    {
                        result = getAttrib.First().Value;
                    }
                    return true;
                }
                else
                {
                    Type xet = typeof(XElement);
                    MemberInfo[] members = xet.GetMember(name);
                    if (members != null && members.Count() > 0)
                    {
                        MemberInfo one = members[0];
                        if (one is PropertyInfo)
                        {
                            var property = (PropertyInfo)one;
                            result = property.GetValue(_InternalNode, null);
                            return true;
                        }
                        else if (one is MethodInfo)
                        {
                            var method = (MethodInfo)one;
                            result = method.Invoke(_InternalNode, null);
                            return true;
                        }
                        result = new DynamicXmlElement(new XElement(name));
                        return true;
                    }
                    else
                    {
                        result = new DynamicXmlElement(new XElement(name));
                        return true;
                    }
                }
            }
        }
예제 #8
0
        public void ShouldUseDynamicXmlAsDynamicModel()
        {
            string xmlString = @"<Model name=""Rui""><site>ArtOfNet</site><child name=""Thaïs""/><child name=""Léandre"" /></Model>";
            dynamic model = new DynamicXmlElement(xmlString);

            Assert.AreEqual(model.name,"Rui");
            Assert.AreEqual(model.child.Count,2);
            foreach (dynamic item in model.child)
            {
                Assert.IsNotNull(item.name);
            }
            Assert.AreEqual(model.child[0].name,"Thaïs");
            Assert.AreEqual(model.child[1].name, "Léandre");
        }