コード例 #1
0
        public void GetFullPropertyPathName_FromInstanceNestedProperty_ReturnsFullPath()
        {
            Expression<Func<TestModel, object>> exp = x => x.Nested.Deep;
            var obj = new TestModel
            {
                Id = 1,
                MyString = null,
                CreatedAt = DateTime.MaxValue,
                Nested = new NestedModel
                {
                    Deep = "Going Deep"
                }
            };

            var result = Reflect.OnProperties.GetFullPropertyPathName<TestModel, object>(exp);

            Assert.Equal("Nested.Deep", result);
        }
コード例 #2
0
        public void GetValue_FromInstance_ReturnsValue()
        {
            Expression<Func<TestModel, object>> exp = x => x.MyString;
            var obj = new TestModel
            {
                Id = 1,
                MyString = "The Value"
            };

            var result = Reflect.OnProperties.GetValue<TestModel, object>(obj, exp);

            Assert.Equal("The Value", result);
        }
コード例 #3
0
        public void SetProperty_PublicProperty_SetsProperty()
        {
            var model = new TestModel();
            Reflect.OnProperties.SetProperty<TestModel>(model, x => x.Id, 5);
            Reflect.OnProperties.SetProperty<TestModel>(model, x => x.MyString, "Bob");

            Assert.Equal(5, model.Id);
            Assert.Equal("Bob", model.MyString);
        }
コード例 #4
0
        public void TypeSafeSetProperty_PublicTypeSafeProperty_SetsProperty()
        {
            var model = new TestModel();
            Reflect.OnProperties.TypeSafeSetProperty<TestModel, int>(model, x => x.Id, 5);
            Reflect.OnProperties.TypeSafeSetProperty<TestModel, string>(model, x => x.MyString, "Bob");

            Assert.Equal(5, model.Id);
            Assert.Equal("Bob", model.MyString);
        }
コード例 #5
0
        public void SetProperty_ProtectedProperty_SetsProperty()
        {
            var model = new TestModel();
            Reflect.OnProperties.SetProperty<TestModel>(model, x => x.President, "Clinton");

            Assert.Equal("Clinton", model.President);
        }
コード例 #6
0
        public void SetProperty_PrivateProperty_SetsProperty()
        {
            var model = new TestModel();
            Reflect.OnProperties.SetProperty<TestModel>(model, x => x.Spy, "Bond");

            Assert.Equal("Bond", model.Spy);
        }
コード例 #7
0
        public void NullSafeGetValue_FromInstanceWithNull_ReturnsSetDefaultValue()
        {
            //Expression<Func<TestModel, object>> exp = x => x.MyString;
            var obj = new TestModel
            {
                Id = 1,
                MyString = null
            };

            var result = Reflect.OnProperties.NullSafeGetValue<TestModel, object>(obj, x => x.MyString, "Default");

            Assert.Equal("Default", result);
        }
コード例 #8
0
        public void NullSafeGetValue_FromInstanceNestedProperty_ReturnsValue()
        {
            Expression<Func<TestModel, object>> exp = x => x.Nested.Deep;
            var obj = new TestModel
            {
                Id = 1,
                MyString = null,
                CreatedAt = DateTime.MaxValue,
                Nested = new NestedModel
                {
                    Deep = "Going Deep"
                }
            };

            var result = Reflect.OnProperties.NullSafeGetValue<TestModel, object>(obj, exp, string.Empty);

            Assert.Equal("Going Deep", result);
        }
コード例 #9
0
        public void NullSafeGetValue_FromInstanceBaseDate_ReturnsDate()
        {
            Expression<Func<TestModel, object>> exp = x => x.CreatedAt;
            var obj = new TestModel
            {
                Id = 1,
                MyString = null,
                CreatedAt = DateTime.MaxValue
            };

            var date = Reflect.OnProperties.NullSafeGetValue<TestModel, object>(obj, exp, DateTime.MinValue);

            Assert.Equal(DateTime.MaxValue, date);
        }