我如何使用 GetType().GetProperties() 访问子 class 的属性
How can i access the properties of a sub class using GetType().GetProperties()
是否可以获取class的所有属性,包括
中的子任务
Class Car
Property Speed
property Wheels(3) as Wheel
Class Wheel
Property Size
Property Type
End Class
End Class
如果我使用这个:
Dim ArrayOfProperties() As Reflection.PropertyInfo = Car.GetType().GetProperties()
我可以获取属性 Speed 和 Wheels,但我无法获取 Size 和 Type。我如何获得子 class 属性?
Not in one fell swoop.
获取Type of a type use the GetType运算符。在下面的示例中 Car
是 type 而不是 Car
.
的 instance
Dim properties As PropertyInfo() = GetType(Car.Wheel).GetProperties()
您可以获得所有嵌套的Types of a Type using the GetNestedTypes方法。
For Each t As Type In GetType(Car).GetNestedTypes()
Dim properties As PropertyInfo() = t.GetProperties()
Next
所以您所要做的就是将所有这些属性添加到一个列表中。
Dim all As New List(Of PropertyInfo)
是否可以获取class的所有属性,包括
中的子任务 Class Car
Property Speed
property Wheels(3) as Wheel
Class Wheel
Property Size
Property Type
End Class
End Class
如果我使用这个:
Dim ArrayOfProperties() As Reflection.PropertyInfo = Car.GetType().GetProperties()
我可以获取属性 Speed 和 Wheels,但我无法获取 Size 和 Type。我如何获得子 class 属性?
Not in one fell swoop.
获取Type of a type use the GetType运算符。在下面的示例中 Car
是 type 而不是 Car
.
Dim properties As PropertyInfo() = GetType(Car.Wheel).GetProperties()
您可以获得所有嵌套的Types of a Type using the GetNestedTypes方法。
For Each t As Type In GetType(Car).GetNestedTypes()
Dim properties As PropertyInfo() = t.GetProperties()
Next
所以您所要做的就是将所有这些属性添加到一个列表中。
Dim all As New List(Of PropertyInfo)