Reflection Doesn't Enumerate Everything
-
Reflection is cool, but geez it can be hard to get it to do some things. I want to enumerate all form controls on a user control that inherits from another user control which also has controls on it. Here's some sample code. UserC in this sample is the user control which inherits from another user control.
Dim BindingFlags As Integer = Reflection.BindingFlags.Instance _
Or Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.NonPublic
For Each fi As System.Reflection.FieldInfo In UserC.GetType.GetFields(BindingFlags)
Dim obj As Object = fi.GetValue(UserC)
If obj IsNot Nothing Then
Try
obj.Name()
Catch ex As Exception
End Try
End If
NextThis will list all the form controls on the UserC class designer but not the class it inherits from. You can see them all when debugging. What gives?
-
Reflection is cool, but geez it can be hard to get it to do some things. I want to enumerate all form controls on a user control that inherits from another user control which also has controls on it. Here's some sample code. UserC in this sample is the user control which inherits from another user control.
Dim BindingFlags As Integer = Reflection.BindingFlags.Instance _
Or Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.NonPublic
For Each fi As System.Reflection.FieldInfo In UserC.GetType.GetFields(BindingFlags)
Dim obj As Object = fi.GetValue(UserC)
If obj IsNot Nothing Then
Try
obj.Name()
Catch ex As Exception
End Try
End If
NextThis will list all the form controls on the UserC class designer but not the class it inherits from. You can see them all when debugging. What gives?
Hi, can't you just iterate through the userControl.Controls collection to get what you want? using your example you'll get all the fields in control UserC, not just the ones created by the designer. Or, you could try adding FlattenHierarchy to your binding flags (inherited fields must be protected, not private). Or you could write a recursive loop that examines the .BaseType fields of UserC.GetType() HTH, Rob
"An eye for an eye only ends up making the whole world blind"
-
Hi, can't you just iterate through the userControl.Controls collection to get what you want? using your example you'll get all the fields in control UserC, not just the ones created by the designer. Or, you could try adding FlattenHierarchy to your binding flags (inherited fields must be protected, not private). Or you could write a recursive loop that examines the .BaseType fields of UserC.GetType() HTH, Rob
"An eye for an eye only ends up making the whole world blind"
I was staying away from userControl.Controls so I would not have to recursively loop through container controls (a group box will appear in the .Controls but you have to walk through it's controls to get all the textboxes and such). I did try FlattenHierarchy and it didn't make any difference. What I didn't try was looking a the fields of .BaseType. Thanks for the reply, Tom