Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Generic List of a specified type

I want to create a generic list - but I want to specify the type at runtime - is there a way I can do this? using reflection perhaps?

Something like this...

Public Shared Sub create(ByVal t As Type) 

  Dim myList As New Generic.List(Of t)

End Sub

Thanks in advance

James

like image 829
James Avatar asked Oct 15 '25 18:10

James


1 Answers

If callers know the type, you can make the method itself generic:

Public Shared Sub create(Of t)() 
    Dim myList As New Generic.List(Of t)
End Sub

If callers do not know the type, you'll have to resort to reflection - see the accepted answer to this question for more information.

like image 103
Jeff Sternal Avatar answered Oct 17 '25 10:10

Jeff Sternal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!