New List (Of T) of a Type passed as a parameter

0

I have a form that collects information for a specific table.

The requirements have changed and you can now pick up from another table. The tables have exactly the same structure so I want to take advantage of the form to store in both tables.

I know I could pass the name of the table to the form and do my checks and inserts but what I try is to pass the type of object to use the methods and functions of my classes.

The type is passed as Public Property Clase As Type and the problem I have when I try to make a list of objects of that type Dim lstResumen As New List(Of Clase) .

How could I make a List(Of of a type passed to the form?

I am developing in Vb .Net Framework 4.

    
asked by Jaime Capilla 06.03.2017 в 09:45
source

2 answers

0

To create a list from a type variable you can use Reflection:

Dim listaGenerica As Type = GetType(List(Of )).MakeGenericType(Clase)
Dim a = Activator.CreateInstance(genericList)

Anyway, I do not recommend it. In your case, I think that the best thing is, since the structure of the two classes is the same, that you use a common interface for both and you will get rid of several headaches.

    
answered by 06.03.2017 в 11:09
0

You could solve it by doing your generic class in such a way that you pass the type of parameter with which you are going to work. An example would be this:

Public Class FormularioImpostor(Of T)

    Private ListaGenerica As List(Of T)

    Public Sub FormularioImpostor()
       ListaGenerica = New List(Of T)
     End Sub
End Class

Public Class TipoUno
    Public Function Algo()
        Return "asdasdsad"
    End Function
End Class

Public Class TipoDos
    Public Function Algo()
        Return "asdasdsad"
    End Function
End Class
    
answered by 06.03.2017 в 11:41