如何将对象类型传递给类?

时间:2018-11-30 09:39:55

标签: .net reflection custom-controls

这是我涉及LiteDB的实际案例的摘要。它将问题简化为更广泛的.net问题。

假设我有3个班级:

Public Class Control
    Public Property ID As Integer
    Public Property ControlName As String
End Class

Public Class Controller
    Public Property ID As Integer
    Public Property ControllerName As String
    Public Controls As List(Of Control)
End Class


Public Class Console
    Public Property ID As Integer
    Public Property ConsoleName As String
    Public Controllers As List(Of Controller)
End Class

我通过Controller设计器将ConsoleBindingSources映射到两个单独的Data Source

我有一个Custom Control,其中包含一个ComboBox。对于同时编辑控制器和控制台列表的页面,此实例绑定到List(Of Control)List(Of Controller)上。

现在,我希望自定义控件更新Controller和Console中的相关属性,并向其传递属性ID,我希望使用组合框的SelectedValue进行更新。 Hopefull,下面的伪代码说明了这一点:

'Custom Control with ComboBox:
Public Class UserControl

    Public bs As BindingSource ' Can contain rows of Controller or Console
    Public CollectionName As String

    Dim WithEvents KComboBox As ComboBox

    Private Sub ComboChanged() Handles KComboBox.SelectionChangeCommitted

        ' [PseudoCode]
        DirectCast(bs.Current, Controller).[CollectionName].ID = KComboBox.SelectedValue
        ' Resolves to:
        DirectCast(bs.Current, Controller).[Controls.ID] = KComboBox.SelectedValue
        ' or:
        DirectCast(bs.Current, Console).[Controllers.ID] = KComboBox.SelectedValue

    End Sub

End Class

我将这样设置自定义组合框:

    Dim cbControls As New UserControl With {.bs = ConrollerBS, .CollectionName = "Controls"}
    Dim cbControllers As New UserControl With {.bs = ConsoleBS, .CollectionName = "Controllers"}

我认为这可能涉及Reflection?我试图继续阅读,但是它使我的头顶掉了下来。

如何实现以上目标?

0 个答案:

没有答案