在您的代码中,问题是因为GetProroperty的默认行为只考虑公共属性。
要访问非公共属性,如Protected、Protected Friend或Friend,您需要指定适当的
BindingFlags
.
这样地:
Public MustInherit Class ParentClass
Public Sub Run()
Dim bindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public
Dim protectedProp = Me.GetType().GetProperty("ProtectedProperty", bindingFlags)
If protectedProp IsNot Nothing Then
Console.WriteLine(protectedProp.GetValue(Me).ToString())
End If
Dim protectedFriendProp = Me.GetType().GetProperty("ProtectedFriendProperty", bindingFlags)
If protectedFriendProp IsNot Nothing Then
Console.WriteLine(protectedFriendProp.GetValue(Me).ToString())
End If
Dim friendProp = Me.GetType().GetProperty("FriendProperty", bindingFlags)
If friendProp IsNot Nothing Then
Console.WriteLine(friendProp.GetValue(Me).ToString())
End If
Dim publicProp = Me.GetType().GetProperty("PublicProperty", bindingFlags)
If publicProp IsNot Nothing Then
Console.WriteLine(publicProp.GetValue(Me).ToString())
End If
End Sub
End Class
添加正确的绑定标志应该能解决这个问题。
请注意,Protected属性是可访问的,因为它是由父类继承的,并且
BindingFlags.NonPublic
在搜索中包括受保护的成员。
朋友和受保护的朋友属性是可访问的,因为
绑定标志。非公开
只要它们在同一组件内,就允许访问它们。
这种方式应该使您的ParentClass能够使用反射访问ChildClass的所有属性,而不管它们的访问级别如何。