如何通过反射访问属性?

时间:2014-02-28 21:49:09

标签: vb.net reflection

我有一些Devexpress控件。

我试图使用反射来设置一些属性。

让我们说TextEdit。

通常,要设置文本框的边框颜色,我会这样做:

TextEdit1.Properties.Appearance.BorderColor = whatever...

使用反思我尝试:

SetProperty(TextEdit1, "Properties.Appearance.BorderColor", RGB(200, 200, 200))

'Where SetProperty sub is
Private Sub SetProperty(ByVal Control As Object, ByVal PropertyName As String, ByVal Args As Object)
    If Not IsNothing(Control.GetType.GetProperty(PropertyName)) Then
        CallByName(Control, PropertyName, CallType.Set, Args)
    End If
End Sub

此代码不起作用,因为 Control.GetType.GetProperty(PropertyName)的结果始终为null。

有谁知道如何访问这个"复合物"属性类型?

由于

1 个答案:

答案 0 :(得分:0)

你必须做这样的事情:

Dim prop = TextEdit1.GetType.GetProperty("Properties").GetValue(TextEdit1)
Dim appe = prop.GetType.GetProperty("Appearance").GetValue(prop)
appe.GetType.GetProperty("BorderColor").SetValue(appe, Color.Red)