Questions tagged «setvalue»

12
通过反射使用字符串值设置属性
我想通过反射设置对象的属性,其值为type string。因此,例如,假设我有一个Ship类,其属性为Latitude,它是一个double。 这是我想做的: Ship ship = new Ship(); string value = "5.5"; PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude"); propertyInfo.SetValue(ship, value, null); 照原样,这引发了ArgumentException: 类型'System.String'的对象不能转换为'System.Double'类型。 如何基于将值转换为适当的类型propertyInfo?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.