public object Bind(object obj) { var type = obj.GetType(); var iProgramTool = ToolFactory.CreateIProgramTool(); foreach (var propertyPath in this._NameValueCollection.AllKeys) { if (string.IsNullOrEmpty(propertyPath)) { continue; } var nvValues = this._NameValueCollection.GetValues(propertyPath); if (nvValues == null || nvValues.Length == 0) { continue; } var bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty; var property = ReflectorHelper.GetPropertyInfoOrDefault(type, propertyPath, bindingFlags); if (property == null) { continue; } object realValue = null; var propertyType = property.PropertyType; try { realValue = iProgramTool.ChanageType(nvValues, property.PropertyType); } catch { } if (realValue != null) { ReflectorHelper.SetProperty(obj, propertyPath, realValue, null); } } return(obj); }
// // 编写测试时,可以使用以下附加特性: // // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码 // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码 // [ClassCleanup()] // public static void MyClassCleanup() { } // // 在运行每个测试之前,使用 TestInitialize 来运行代码 // [TestInitialize()] // public void MyTestInitialize() { } // // 在每个测试运行完之后,使用 TestCleanup 来运行代码 // [TestCleanup()] // public void MyTestCleanup() { } // #endregion public void TestMethod1() { var parent = new Parent() { Name = "Parent" }; var getPropertyResult = ReflectorHelper.GetProperty(parent, "name"); Assert.AreEqual("Parent", getPropertyResult); ReflectorHelper.SetProperty(parent, "name", "name1"); var getPropertyResult1 = ReflectorHelper.GetProperty(parent, "name"); Assert.AreEqual("name1", getPropertyResult1); ReflectorHelper.SetProperty(parent, "child.name", "childname"); var getPropertyResultChildName = ReflectorHelper.GetProperty(parent, "child.name"); Assert.AreEqual("childname", getPropertyResultChildName); }