Type of Generic seems worthless

Please see the code below:

Public Function ExecuteDynamicQuery(Of T As New)(ByVal sql As String, ByVal type As T) As List(Of T) Implements IGenie.ExecuteDynamicQuery
            Dim iConnectionBLL As iConnectionBLL = New clsConnectionBLL
            Dim paramValues() As DbParameter = New clsParameterValues().getParameterValues()
            Using conn As DbConnection = iConnectionBLL.getDatabaseTypeByDescription("Genie2"), _
                  rdr As DbDataReader = clsDatabaseHelper.ExecuteReader(conn, CommandType.Text, sql, paramValues)
                Dim list As List(Of T) = New List(Of T)
                While rdr.Read()
                    Dim hello As New T
                    Dim method As MethodInfo = GetType(clsType).GetMethod("PopulateDataReader")
                    method.Invoke(hello, New Object() {rdr})
                    list.Add(hello)
                End While

                Return list
            End Using
        End Function

Is there a way of executing the SQL statement above without passing in type as an arguement. It seems a bit pointless - the only reason it is there is to let the function know the type of the generic.

Jon Skeet
people
quotationmark

Well you can change the method to not have the second parameter:

Public Function ExecuteDynamicQuery(Of T As New)(ByVal sql As String) As List(Of T) Implements IGenie.ExecuteDynamicQuery

However:

  • You'd need to change IGenie as well
  • The caller would then need to explicitly specify the type argument, instead of letting the compiler infer it on the basis of the argument (which would no longer be present)

people

See more on this question at Stackoverflow