T sub的通用集合不起作用

时间:2014-03-13 01:38:37

标签: vb.net generics

我试图教自己的泛型并尝试创建一个通用列表(T)。在这种情况下,T将是我定义的对象类。在尝试在泛型类型中设置属性之前,一切似乎都正常工作。

我得到"对象引用未设置为对象的实例"在这一行。 tType.GetProperty(ColumnName).SetValue(ListItemObject, ConvertedValue, Nothing)

这是我在T对象中继承的子。所以我会这样称呼它。 T.GetCollectionFromDB(),然后它将设置T(me)与集合数据。我无法弄清楚为什么我得到对象引用错误。也许你经验丰富的专业人士可以让我知道为什提前谢谢。

    Public Overloads Sub GetCollectionFromDB(ByVal ID As Integer)
        Try
            Dim BaseObject As New Object
            Dim tType As Type = GetType(t)
            Dim ListCollection As New List(Of t)
            Dim ListItemObject As Object
            Dim BaseType As Type = MyBase.GetType()
            Dim TableName As String = String.Empty
            Dim SQL As New LottoPayload.DataHandler.SQLiDataClass
            Dim SQLString As String = String.Empty
            Dim SQLCommand As SQLiteCommand = Nothing
            Dim SQLConnection As SQLiteConnection = Nothing
            Dim SQLiteDRObj As SQLiteDataReader = Nothing
            Dim SQLResultsTable As New DataTable

            'Create an instance of the base object
            BaseObject = Activator.CreateInstance(BaseType)
            'Get the tablename from the object
            If Not BaseObject Is Nothing Then
                TableName = BaseType.GetProperty("TableName").GetValue(BaseObject, Nothing).ToString
                If Not TableName Is Nothing Then
                    SQLString = "SELECT * FROM " & TableName & " WHERE GameID = '" & ID.ToString & "'"
                    SQLConnection = SQL.GetSqlConnection()

                    SQLConnection.Open()
                    SQLCommand = New SQLiteCommand(SQLConnection)
                    SQLCommand.CommandText = SQLString

                    SQLiteDRObj = SQLCommand.ExecuteReader()

                    SQLResultsTable.Load(SQLiteDRObj)
                    If SQLResultsTable.Rows.Count > 0 Then
                        For Each Row As DataRow In SQLResultsTable.Rows
                            'Create a new object
                            ListItemObject = Activator.CreateInstance(tType)
                            For Each Column As DataColumn In Row.Table.Columns

                                Dim ColumnName As String = Column.ColumnName.ToString
                                'Console.WriteLine(Row.Item(ColumnName))
                                Dim ColumnValue As Object = Row.Item(ColumnName)
                                'Console.WriteLine(Column.DataType.Name & "    ")
                                Select Case Column.DataType.Name
                                    Case "Int64"
                                        Dim ConvertedValue As Integer
                                        Integer.TryParse(ColumnValue.ToString, ConvertedValue)
                                        tType.GetProperty(ColumnName).SetValue(ListItemObject, ConvertedValue, Nothing)
                                    Case "Double"
                                        Dim ConvertedValue As Double
                                        Double.TryParse(ColumnValue.ToString, ConvertedValue)
                                        tType.GetProperty(ColumnName).SetValue(ListItemObject, ConvertedValue, Nothing)
                                    Case "String"
                                        Dim ConvertedValue As String
                                        ConvertedValue = ColumnValue.ToString
                                        tType.GetProperty(ColumnName).SetValue(ListItemObject, ConvertedValue, Nothing)
                                    Case Else
                                        Dim ConvertedValue As String
                                        ConvertedValue = "NotSet"
                                        tType.GetProperty(ColumnName).SetValue(ListItemObject, ConvertedValue, Nothing)

                                End Select

                            Next

                            ListCollection.Add(DirectCast(ListItemObject, t))
                            ListItemObject = Nothing
                        Next

                    End If


                    SQLiteDRObj.Close()
                    SQLConnection.Close()
                    SQLConnection.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally

        End Try
    End Sub

0 个答案:

没有答案