如何动态获取对象类型并转换为它?
How do I dynamically get an object type and cast to it?
如何获取对象类型以便直接转换为它?这是我想要执行的理想方法:
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
我正在尝试使代码更高效,这样我就不必直接转换为指定的对象类型。例如:
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
编辑
据我所知,ToolStripItem
(ToolStripButton) 不是 Control
,因此我不能在这种情况下使用 List(Of Control)
。当我第一次使用控件列表时,未包含工具条项。这是我第一次在应用程序中使用 ToolStrip
,所以直到现在我都没有理由不使用 List(Of Control)
。
所有控件都源自 Control
。因此,不要使用类型 Object
,而是使用 Control
。 Control
拥有这些控件的大多数成员,如 Click
事件。
Dim myControls As New List(Of Control)
For Each ctrl As Control In _
GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
AddHandler ctrl.Click, AddressOf GotFocus
Next
在 FindControlsRecursive
中也使用 Control
。
参见:
原来你有一些组件不是控件。但是您仍然可以将所有控件转换为 Control
Dim myControls As New List(Of Object)
For Each obj As Object In
GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
Select Case True
Case TypeOf obj Is Control
AddHandler DirectCast(obj, Control).Click, AddressOf GotFocus
Case TypeOf obj Is ToolStripItem
AddHandler DirectCast(obj, ToolStripItem).Click, AddressOf GotFocus
End Select
Next
请注意 ToolStripItem
包括 ToolStripButton
、ToolStripControlHost
、ToolStripDropDownItem
、ToolStripLabel
和 ToolStripSeparator
,因为所有这些组件都来自ToolStripItem
。您可以在 Visual Studio 的对象浏览器中看到:
MenuStrip
是一个 Control
。因此,这两个案例应该涵盖您的大部分控件和组件。如果您发现此处未涵盖的另一个组件,请搜索其具有 Click
事件的最少派生基类型,以便新案例涵盖尽可能多的组件。
如何获取对象类型以便直接转换为它?这是我想要执行的理想方法:
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
我正在尝试使代码更高效,这样我就不必直接转换为指定的对象类型。例如:
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
编辑
据我所知,ToolStripItem
(ToolStripButton) 不是 Control
,因此我不能在这种情况下使用 List(Of Control)
。当我第一次使用控件列表时,未包含工具条项。这是我第一次在应用程序中使用 ToolStrip
,所以直到现在我都没有理由不使用 List(Of Control)
。
所有控件都源自 Control
。因此,不要使用类型 Object
,而是使用 Control
。 Control
拥有这些控件的大多数成员,如 Click
事件。
Dim myControls As New List(Of Control)
For Each ctrl As Control In _
GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
AddHandler ctrl.Click, AddressOf GotFocus
Next
在 FindControlsRecursive
中也使用 Control
。
参见:
原来你有一些组件不是控件。但是您仍然可以将所有控件转换为 Control
Dim myControls As New List(Of Object)
For Each obj As Object In
GlobalFunctions.GeneralFunctions.FindControlsRecursive(myControls, Form)
Select Case True
Case TypeOf obj Is Control
AddHandler DirectCast(obj, Control).Click, AddressOf GotFocus
Case TypeOf obj Is ToolStripItem
AddHandler DirectCast(obj, ToolStripItem).Click, AddressOf GotFocus
End Select
Next
请注意 ToolStripItem
包括 ToolStripButton
、ToolStripControlHost
、ToolStripDropDownItem
、ToolStripLabel
和 ToolStripSeparator
,因为所有这些组件都来自ToolStripItem
。您可以在 Visual Studio 的对象浏览器中看到:
MenuStrip
是一个 Control
。因此,这两个案例应该涵盖您的大部分控件和组件。如果您发现此处未涵盖的另一个组件,请搜索其具有 Click
事件的最少派生基类型,以便新案例涵盖尽可能多的组件。