Vb.net 如何使用反射设置值类型(例如点)的属性?

Vb.net 如何使用反射设置值类型(例如点)的属性?,vb.net,reflection,properties,Vb.net,Reflection,Properties,我们在运行时动态创建了一个新的Drawing.Point,效果很好。现在我们要在运行时设置属性“X”和“Y”。 我们试着这样做: Public Function SetObjectProperty(propertyName As String, value As Integer, refObj As Object) Dim propertyInfo As PropertyInfo = refObj.GetType().GetProperty(propertyName)

我们在运行时动态创建了一个新的
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
但它不起作用。属性未使用值设置。
我们遗漏了什么吗?

该值必须是Drawing.Point()类型,而不是整数。 你可以利用像

Public Function SetObjectProperty(propertyName As String, value As Point, refObj As Object)

Dim propertyInfo As PropertyInfo = refObj.GetType().GetProperty(propertyName)

If propertyInfo IsNot Nothing Then

    propertyInfo.SetValue(refObj, value.X, Nothing)
    Return refObj
End If
Return Nothing

在上面,您可以使用value.X甚至仅使用value来获取两个坐标。

该值必须是Drawing.Point()类型,而不是整数。 你可以利用像

Public Function SetObjectProperty(propertyName As String, value As Point, refObj As Object)

Dim propertyInfo As PropertyInfo = refObj.GetType().GetProperty(propertyName)

If propertyInfo IsNot Nothing Then

    propertyInfo.SetValue(refObj, value.X, Nothing)
    Return refObj
End If
Return Nothing

在上面,您可以使用value.X或甚至仅使用value来获取两个坐标。

问题在于
System.Drawing.Point
是一种值类型。当您将此值传递到
设置值
中时,它将被装箱。已装箱对象上的值已更改,但原始值未更改。下面是一个在更改值之前进行装箱的修改。您还需要
ByRef
参数修饰符:

Public Function SetObjectProperty(propertyName As String, value As Integer, ByRef refObj As Object)
    Dim type = refObj.GetType()
    Dim propertyInfo As PropertyInfo = type.GetProperty(propertyName)

    If propertyInfo IsNot Nothing Then
        If type.IsValueType Then
            Dim boxedObj As ValueType = refObj
            propertyInfo.SetValue(boxedObj, 25)
            refObj = boxedObj
        Else
            propertyInfo.SetValue(refObj, value)
        End If
        Return refObj
    End If
    Return Nothing
End Function
您可以像以前一样使用它:

Dim p As Point
SetObjectProperty("X", 25, p)

顺便说一句,想想你是否真的需要返回值。这似乎不是必需的。

问题在于
System.Drawing.Point
是一种值类型。当您将此值传递到
设置值
中时,它将被装箱。已装箱对象上的值已更改,但原始值未更改。下面是一个在更改值之前进行装箱的修改。您还需要
ByRef
参数修饰符:

Public Function SetObjectProperty(propertyName As String, value As Integer, ByRef refObj As Object)
    Dim type = refObj.GetType()
    Dim propertyInfo As PropertyInfo = type.GetProperty(propertyName)

    If propertyInfo IsNot Nothing Then
        If type.IsValueType Then
            Dim boxedObj As ValueType = refObj
            propertyInfo.SetValue(boxedObj, 25)
            refObj = boxedObj
        Else
            propertyInfo.SetValue(refObj, value)
        End If
        Return refObj
    End If
    Return Nothing
End Function
您可以像以前一样使用它:

Dim p As Point
SetObjectProperty("X", 25, p)

顺便说一句,想想你是否真的需要返回值。这似乎没有必要。

为什么不直接调用pointVariable.X=10呢?因为我们被告知要保持通用性。现在,我们正在创建与读取器从xml文件中获取的内容动态相关的对象。所以我们不能这样编码,因为在下一个循环中,可能有另一个方法(如“Color”)需要设置点对象的属性以外的其他内容:/I我刚刚在控制台中看到propertyInfo的值是“Int32 X”,而不仅仅是“X”这就是问题所在吗?我们如何解决它?为什么不直接调用pointVariable.X=10呢?因为我们被告知要保持通用性。现在,我们正在创建与读取器从xml文件中获取的内容动态相关的对象。所以我们不能这样编码,因为在下一个循环中,可能有另一个方法(如“Color”)需要设置点对象的属性以外的其他内容:/I我刚刚在控制台中看到propertyInfo的值是“Int32 X”,而不仅仅是“X”这可能是问题所在,我们如何解决?好的,谢谢你,这看起来正是我们需要的。但是你能解释一下SetValue参数吗?此时,“REFBJ”是我们的Drawing.point-Object,其值为整数。propertyInfo是应该像“X”一样更改的属性。我们需要在这里改变什么?更改图形的属性是否正确。点我首先需要创建图形。点?:/难道没有一种方法可以在其中得到一个整数值吗?原因是对象是一个绘图点类型,SetValue的“值”必须实现这一点。尽管“X”和“Y”是整数/Int32,尽管方法“SetValue”也表示整数,但您必须尊重“refObj”的类型,如果使用VALUE.X,则将传递该结构(X)的整数部分。Drawing.Point类型对象需要图形点结构,即使仅使用其一个坐标(整数)。表单中的相似位置和位置:它们需要“类似于坐标的位置”类型才能工作。好的。但我不能让它真正充满活力。因为我在SetValue中调用的Drawing.Point必须在之前声明过,这就是我们现在面临的问题。根据从xml中获得的内容设置动态属性:/六羟甲基三聚氰胺六甲醚。。。我并不完全理解,但我将尝试解决:您必须声明点类型的变量,例如:Dim Sample as point=Nothing。在运行时,两个坐标可能都是整数。如果是这样的话,您只需在设置属性之前进行如下操作:Sample.X=Integer1,Sample.Y=Integer2。是吗?谢谢大卫:)我真的很感谢你的帮助。但我认为我们解决了问题,就像尼科说的,这是盒装物体的问题。感谢您抽出时间,感谢您的帮助:)好的,谢谢您,这看起来正是我们需要的。但是你能解释一下SetValue参数吗?此时,“REFBJ”是我们的Drawing.point-Object,其值为整数。propertyInfo是应该像“X”一样更改的属性。我们需要在这里改变什么?更改图形的属性是否正确。点我首先需要创建图形。点?:/难道没有一种方法可以在其中得到一个整数值吗?原因是对象是一个绘图点类型,SetValue的“值”必须实现这一点。尽管“X”和“Y”是整数/Int32,尽管方法“SetValue”也表示整数,但您必须尊重“refObj”的类型,如果使用VALUE.X,则将传递该结构(X)的整数部分。Drawing.Point类型对象需要图形点结构,即使仅使用其一个坐标(整数)。表单中的相似位置和位置:它们需要“类似于坐标的位置”类型才能工作。好的。但我不能让它真正充满活力。因为我在SetValue中调用的Drawing.Point必须在之前声明过,这就是我们现在面临的问题。根据从xml中获得的内容设置动态属性:/六羟甲基三聚氰胺六甲醚。。。我并不完全理解,但我将尝试解决:您必须声明点类型的变量,例如:Dim Sample as point=Nothing。在运行时,两个坐标可能都是整数。如果是这样,您只需在设置属性Sample.X=Integer1,Sample.Y之前进行如下操作=