Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Reflection Doesn't Enumerate Everything

Reflection Doesn't Enumerate Everything

Scheduled Pinned Locked Moved .NET (Core and Framework)
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thomas Wells
    wrote on last edited by
    #1

    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
    Next

    This 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?

    R 1 Reply Last reply
    0
    • T Thomas Wells

      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
      Next

      This 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?

      R Offline
      R Offline
      Rob Smiley
      wrote on last edited by
      #2

      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"

      T 1 Reply Last reply
      0
      • R Rob Smiley

        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"

        T Offline
        T Offline
        Thomas Wells
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups