the examples for .Metadata()

 [TestMethod]
        public void get_property_value()
        {
            var obj = new { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };


            var name = obj.Metadata().Properties["Name"].GetValue(obj);
            Assert.AreEqual(obj.Name, name);

            var blog = obj.Metadata().Properties.Where(i => i.Name == "Blog").Select(i => i.GetValue(obj)).FirstOrDefault();
            Assert.AreEqual(obj.Blog, blog);
        }

        [TestMethod]
        public void set_property_value()
        {

            var obj = new MockObject { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };

             var changedLocation = "Xiamen,China";

             obj.Metadata().Properties["Location"].SetValue(obj,changedLocation);           
           

            Assert.AreEqual(changedLocation, obj.Location);
        }

        [TestMethod]
        public void get_properties_contains_the_custom_attribute()
        {

            var obj = new MockObject { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };


            var property = obj.Metadata().Properties
                .Where(i => i.Attributes.Any(a => a.Attribute is MockAttribute));
        
            
            Assert.IsTrue(property.Any(i=>i.Name == "Name"));
            Assert.IsTrue(property.Any(i=>i.Name == "Location"));
        }

        [TestMethod]
        public void get_custom_attribute()
        {
            var obj = new MockObject { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };


            var attribute = obj.Metadata().Properties
                .Where(i => i.Name == "Name")
                .FirstOrDefault()
                .Attributes
                .Select(i => i.Attribute)
                .OfType<MockAttribute>()
                .SingleOrDefault();

            Assert.AreEqual("this is the name", attribute.Name);
        }

        [TestMethod]
        public void get_field_value()
        {
            var obj = new MockObject();

            var field = obj.Metadata().Fields["Country"];

            Assert.AreEqual("China", field.GetValue(obj));
        }

        [TestMethod]
        public void invoke_method()
        {
            var obj = new MockObject();

            var method = obj.Metadata().Methods["SayHello"];

            Assert.AreEqual("Hi,world",method.Invoke(obj,new []{"world"}));
        }

Last edited Aug 29, 2010 at 12:16 PM by cnlangzi, version 1

Comments

No comments yet.