title: How do I dynamically get an object type and cast to it?
body: <p>How do I get the object type so I can directly cast to it? This is the ideal method I would like to execute:</p>
  <pre><code>Dim MyObjects As New List(Of Object)
  For Each O As Object In GlobalFunctions.GeneralFunctions.FindControlsRecursive(MyObjects, Form)
      Select Case True
          Case TypeOf O Is MenuStrip Or TypeOf O Is ToolStripButton Or TypeOf O Is Panel Or TypeOf O Is Label Or TypeOf O Is ToolStripSeparator
              AddHandler DirectCast(O, O.GetType).Click, AddressOf GotFocus
      End Select
  Next
  </code></pre>
  <p>I am trying to make the code more efficient so that I do not have to directly cast to a specified object type. Ex.:</p>
  <pre><code>Dim MyObjectsAs New List(Of Object)
  For Each O As Object In GlobalFunctions.GeneralFunctions.FindControlsRecursive(MyObjects, Form)
      Select Case True
          Case TypeOf O Is MenuStrip
              AddHandler DirectCast(O, MenuStrip).Click, AddressOf GotFocus
          Case TypeOf O Is Panel
              AddHandler DirectCast(O, Panel).Click, AddressOf GotFocus
          Case TypeOf O Is ToolStripButton
              AddHandler DirectCast(O, ToolStripButton).Click, AddressOf GotFocus
          Etc...
      End Select
  Next 
  </code></pre>
  <hr />
  <h1>EDIT</h1>
  <p>To my knowledge, a <code>ToolStripItem</code> (<em>ToolStripButton</em>) is not a <code>Control</code> so I cannot use a <code>List(Of Control)</code> for this situation. When I first was using a list of controls, the toolstrip items were not being included. This is the first time I have used <code>ToolStrip</code> in an application so I never had a reason for not using <code>List(Of Control)</code> until now.</p>