如何使用List(of Class)参数设置构造函数

时间:2014-05-05 09:25:42

标签: vb.net class

鉴于我正在构建数据包装器的情况,我传递给jquery数据表,例如,我有三个Classes Orders,OrderDetails和Customers。在数据表中,对象列表由变量名aoData表示,因此要构造我必须传递List(Of someclass)。因为在我通过之前我不知道哪个班级你怎么做?有一个GetType,它将类名作为字符串?我找不到一个。

 Public Class DataTablesWrapper
    Public Sub New(ByRef data As List(Of String))
        Me.aaData = data
    End Sub
    Public Sub New(ByRef data As List(Of some class depending on what data I want wrapped))
        Me.aoData = data
    End Sub
    Public Property aaData As List(Of String)
    Public Property aoData As List(Of some class depending on what data I want wrapped)
End Class

1 个答案:

答案 0 :(得分:1)

我建议使用generics

在你的情况下,它看起来像这样:

Public Class DataTablesWrapper(Of T)
    Public Sub New(ByRef data As List(Of T))
        Me.aaData = data
    End Sub

    Public Property aaData As List(Of T)
End Class

创建您的实例将如下所示:

Sub Main()
    Dim stringList As New List(Of String)({"foo", "bar"})      
    Dim tablesWrapper As New DataTablesWrapper(Of String)(stringList)
End Sub

由于this reasons,您不想使用List(Of Object)