public void CreateSetWithBadObjectTarget()
        {
            ExceptionAssert.Throws <InvalidCastException>(
                () =>
            {
                Person p = new Person();
                Movie m  = new Movie();

                Action <object, object> setter =
                    ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                        TestReflectionUtils.GetProperty(typeof(Movie), "Name")
                        );

                setter(m, "Hi");

                Assert.AreEqual(m.Name, "Hi");

                setter(p, "Hi");

                Assert.AreEqual(p.Name, "Hi");
            },
                new[]
            {
                "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Organization.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
                "Cannot cast from source type to destination type."     // mono
            }
                );
        }
        public void CreatePropertySetter()
        {
            Action <object, object> setter =
                ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                    TestReflectionUtils.GetProperty(typeof(Movie), "Name")
                    );

            Movie m = new Movie();

            setter(m, "OH HAI!");

            Assert.AreEqual("OH HAI!", m.Name);
        }
        public void CreatePropertyGetter()
        {
            Func <object, object> getter =
                ExpressionReflectionDelegateFactory.Instance.CreateGet <object>(
                    TestReflectionUtils.GetProperty(typeof(Movie), "Name")
                    );

            Movie m = new Movie();

            m.Name = "OH HAI!";

            object value = getter(m);

            Assert.AreEqual("OH HAI!", value);
        }
        public void SetStatic()
        {
            Action <object, object> setter =
                ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                    TestReflectionUtils.GetProperty(typeof(StaticTestClass), "StringProperty")
                    );

            setter(null, "New property!");
            Assert.AreEqual("New property!", StaticTestClass.StringProperty);

            setter = ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                TestReflectionUtils.GetField(typeof(StaticTestClass), "StringField")
                );

            setter(null, "New field!");
            Assert.AreEqual("New field!", StaticTestClass.StringField);
        }
        public void SetOnStruct()
        {
            object structTest = new StructTest();

            Action <object, object> setter =
                ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                    TestReflectionUtils.GetProperty(typeof(StructTest), "StringProperty")
                    );

            setter(structTest, "Hi1");
            Assert.AreEqual("Hi1", ((StructTest)structTest).StringProperty);

            setter = ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                TestReflectionUtils.GetField(typeof(StructTest), "StringField")
                );

            setter(structTest, "Hi2");
            Assert.AreEqual("Hi2", ((StructTest)structTest).StringField);
        }
        public void CreateSetWithBadObjectValue()
        {
            ExceptionAssert.Throws <InvalidCastException>(
                () =>
            {
                Movie m = new Movie();

                Action <object, object> setter =
                    ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(
                        TestReflectionUtils.GetProperty(typeof(Movie), "Name")
                        );

                setter(m, new Version("1.1.1.1"));
            },
                new[]
            {
                "Unable to cast object of type 'System.Version' to type 'System.String'.",
                "Cannot cast from source type to destination type."     //mono
            }
                );
        }
        public void GetStatic()
        {
            StaticTestClass.StringField    = "Field!";
            StaticTestClass.StringProperty = "Property!";

            Func <object, object> getter =
                ExpressionReflectionDelegateFactory.Instance.CreateGet <object>(
                    TestReflectionUtils.GetProperty(typeof(StaticTestClass), "StringProperty")
                    );

            object v = getter(null);

            Assert.AreEqual(StaticTestClass.StringProperty, v);

            getter = ExpressionReflectionDelegateFactory.Instance.CreateGet <object>(
                TestReflectionUtils.GetField(typeof(StaticTestClass), "StringField")
                );

            v = getter(null);
            Assert.AreEqual(StaticTestClass.StringField, v);
        }