Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a object of Type T in vb.net

Initially I was coding in C#.

Recently started with vb.net for my new project.

In C# reflection method, I used to create a object of generic type T as such

T item= new T()

Can anybody please suggest how can I achieve the same thing in VB.Net

Dim item As new Type 

is not compiling . Please help

like image 979
chaithra manoj Avatar asked Feb 20 '14 09:02

chaithra manoj


1 Answers

If your generic type parameter is T then:

Dim item As new T

Note though that you'll need to have the New type constraint on the type parameter:

Public Class Foo(Of T As New)

    Public Function Bar() As T

        Dim item As New T
        ...
        Return item

    End Function

End Class
like image 173
Jon Egerton Avatar answered Nov 07 '22 00:11

Jon Egerton