Spring.net ObjectWrapper对象的包装(反射机制)有点明晰方便

            Company c = new Company();
            List<PropertyInfo> ps = c.GetType().GetProperties().ToList();
            var pname = ps.First(x => x.Name == "Name");
            IObjectWrapper owComp = new ObjectWrapper(c);
            // setting the company name...
            pname.SetValue(c, "134");
            owComp.SetPropertyValue("Name", "KEXB.");
            // can also be done like this...
            PropertyValue v = new PropertyValue("name", "Salina Inc1.");
            owComp.SetPropertyValue(v);
            // ok, let's create the director and bind it to the company...
            Employee don = new Employee();
            IObjectWrapper owDon = new ObjectWrapper(don);
            owDon.SetPropertyValue("salary", 99);
            owDon.SetPropertyValue("name", "Don Fabrizio");
            owComp.SetPropertyValue("managingDirector", don);
            // retrieving the salary of the ManagingDirector through the company
            float salary = (float)owComp.GetPropertyValue("ManagingDirector.salary");

 

posted on 2016-09-29 21:43  听哥哥的话  阅读(270)  评论(0编辑  收藏  举报

导航