コード例 #1
0
        public void SetValueByPath_Test()
        {
            var o1 = new ReflectionUtilClass2()
            {
                Id       = "1000",
                UserName = "******",
                Age      = 18,
                Class1   = new ReflectionUtilClass1()
                {
                    UserName = "******"
                }
            };

            Assert.Equal("1000", o1.Id);
            Assert.Equal("zhangsan", o1.UserName);
            Assert.Equal(18, o1.Age);
            ReflectionUtil.SetValueByPath(o1, typeof(ReflectionUtilClass2), "Id", "3");
            Assert.Equal("3", o1.Id);
            ReflectionUtil.SetValueByPath(o1, typeof(ReflectionUtilClass2), "DotCommon.Test.Reflecting.ReflectionUtilClass2.UserName", "lisi");
            Assert.Equal("lisi", o1.UserName);


            ReflectionUtil.SetValueByPath(o1, typeof(ReflectionUtilClass2), "Class1.UserName", "123");
            Assert.Equal("123", o1.Class1.UserName);
        }
コード例 #2
0
        public void GetValueByPath_Test()
        {
            var o1 = new ReflectionUtilClass2()
            {
                Id       = "1000",
                UserName = "******",
                Age      = 18
            };

            var v1 = ReflectionUtil.GetValueByPath(o1, typeof(ReflectionUtilClass2), "Age");

            Assert.Equal(18, Convert.ToInt32(v1));
            var v2 = ReflectionUtil.GetValueByPath(o1, typeof(ReflectionUtilClass2), "DotCommon.Test.Reflecting.ReflectionUtilClass2.Age");

            Assert.Equal(18, Convert.ToInt32(v2));
            var v3 = ReflectionUtil.GetValueByPath(o1, typeof(ReflectionUtilClass2), "Id");

            Assert.Equal("1000", v3.ToString());
        }
コード例 #3
0
        public void GetPropertyByPath_Test()
        {
            var o1 = new ReflectionUtilClass2()
            {
                Id       = "1000",
                UserName = "******",
                Age      = 18
            };

            var p1 = ReflectionUtil.GetPropertyByPath(o1, typeof(ReflectionUtilClass2), "Id");

            Assert.Equal(typeof(ReflectionUtilClass2).GetProperty("Id"), p1);
            var p2 = ReflectionUtil.GetPropertyByPath(o1, typeof(ReflectionUtilClass2), "DotCommon.Test.Reflecting.ReflectionUtilClass2.Id");

            Assert.Equal(typeof(ReflectionUtilClass2).GetProperty("Id"), p2);
            var p3 = ReflectionUtil.GetPropertyByPath(o1, typeof(ReflectionUtilClass2), "UserName");

            Assert.Equal(typeof(ReflectionUtilClass2).GetProperty("UserName"), p3);
            var p4 = ReflectionUtil.GetPropertyByPath(o1, typeof(ReflectionUtilClass2), "Age");

            Assert.Equal(typeof(ReflectionUtilClass2).GetProperty("Age"), p4);
        }