简体   繁体   English

如何动态获取 object 类型并转换为它?

[英]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 

EDIT编辑

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包括ToolStripButtonToolStripControlHostToolStripDropDownItemToolStripLabelToolStripSeparator ,因为所有这些组件都派生自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.

相关问题 如何解决“无法将类型'ASP.addtoroster_aspx'的对象强制转换为'System.Web.UI.WebControls.GridViewRow'错误? - How do I resolve "Unable to cast object of type 'ASP.addtoroster_aspx' to type 'System.Web.UI.WebControls.GridViewRow' error? 如何将 C# Object Cast 转换为 VB.net Object Cast?(显示了特定的 Cast 方法) - How Do I convert C# Object Cast to VB.net Object Cast?(Specific Cast Method Shown) 如何将对象强制转换为在运行时提取的Type - How to cast an object to a Type extracted at runtime 如何将对象强制转换为struct类型的可空值? - How to cast an object to a nullable of type struct? 如何将_ComObject类型转换为本机类型,例如Long或其他类型(出现强制转换错误)? - How do I convert type _ComObject to a native type like Long or other (getting cast error)? 如何将对象列表转换为对象在VB.net中实现的接口列表? - How do I cast a list of an object to a list of an interface that the object implements in VB.net? 无法转换类型的对象, - Unable to cast object of type, 如何在不将其重新分配给VB.NET中的另一个变量的情况下强制转换对象? - How do I cast an object without re-assigning it to another variable in VB.NET? 如何获取VBCodeProvider返回对象 - How do I get VBCodeProvider to return an object 如何获取多个动态生成的下拉列表的值? - How do I get the value of a multiple dynamically generated dropdownlist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM