[英]How do I dynamically get an object type and cast to it?
How do I get the object type so I can directly cast to it?如何获得 object 类型以便我可以直接转换为它? This is the ideal method I would like to execute:
这是我想执行的理想方法:
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
I am trying to make the code more efficient so that I do not have to directly cast to a specified object type.我试图使代码更高效,这样我就不必直接转换为指定的 object 类型。 Ex.:
前任。:
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
To my knowledge, a ToolStripItem
( ToolStripButton ) is not a Control
so I cannot use a List(Of Control)
for this situation.据我所知,
ToolStripItem
( ToolStripButton ) 不是Control
,因此我不能在这种情况下使用List(Of Control)
。 When I first was using a list of controls, the toolstrip items were not being included.当我第一次使用控件列表时,工具条项目不包括在内。 This is the first time I have used
ToolStrip
in an application so I never had a reason for not using List(Of Control)
until now.这是我第一次在应用程序中使用
ToolStrip
,所以直到现在我才没有理由不使用List(Of Control)
。
All controls derive from Control
.所有控件都派生自
Control
。 Therefore, instead of using the type Object
use Control
.因此,不要使用
Object
类型,而是使用Control
。 Control
has most of the members of these controls like a Click
event. 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
Use Control
in FindControlsRecursive
as well.在
FindControlsRecursive
中也可以使用Control
。
See:看:
It turned out that you have some components not being controls.事实证明,您有一些组件不是控件。 But you can still cast all controls to
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
Note that ToolStripItem
includes ToolStripButton
, ToolStripControlHost
, ToolStripDropDownItem
, ToolStripLabel
and ToolStripSeparator
, since all of these components derive from ToolStripItem
.注意
ToolStripItem
包括ToolStripButton
、 ToolStripControlHost
、 ToolStripDropDownItem
、 ToolStripLabel
和ToolStripSeparator
,因为所有这些组件都派生自ToolStripItem
。 You can see this in the Object Browser in Visual Studio:您可以在 Visual Studio 的 Object 浏览器中看到这一点:
MenuStrip
is a Control
. MenuStrip
是一个Control
。 So, these two cases should cover most of your controls and components.因此,这两种情况应该涵盖您的大部分控件和组件。 If you find another component not covered here, search for its least derived base type featuring the
Click
event, so that the new case covers as many components as possible.如果您发现此处未涵盖的另一个组件,请搜索其具有
Click
事件的派生最少的基本类型,以便新案例涵盖尽可能多的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.