【发布时间】:2015-12-06 19:42:58
【问题描述】:
我们在运行时动态创建了一个新的Drawing.Point,它运行良好。现在我们要在运行时设置属性“X”和“Y”。
我们试图这样做:
Public Function SetObjectProperty(propertyName As String, value As Integer, refObj As Object)
Dim propertyInfo As PropertyInfo = refObj.GetType().GetProperty(propertyName)
If propertyInfo IsNot Nothing Then
propertyInfo.SetValue(refObj, value, Nothing)
Return refObj
End If
Return Nothing
End Function
但它没有用。未使用值设置属性。 我们错过了什么吗?
【问题讨论】:
-
为什么不直接调用 pointVariable.X = 10 ?
-
因为我们被告知要保持通用性。现在我们正在创建与阅读器从 xml 文件中获取的内容动态相关的对象。所以我们不能这样编码,因为在下一个循环中可能有另一种方法(如“颜色”)需要设置其他东西,然后是点对象的属性:/我刚刚在控制台中读到value propertyInfo 是 "Int32 X" 而不是只有 "X" 这可能是问题所在,我们该如何解决?
标签: vb.net reflection properties