Reflection and changing a variables type at runtime?

Posted by james-west on Stack Overflow See other posts from Stack Overflow or by james-west
Published on 2010-05-26T17:25:56Z Indexed on 2010/05/26 17:31 UTC
Read the original article Hit count: 430

Filed under:
|
|

Hi

I'm trying to create an object Of a specific type. I've got the following code, but it fails because it can't cast the new table object to the one that is already defined. I need table to start of an IEnumerable type so I can't declare is an object.

Public sub getTable(ByVal t as Type)

   Dim table As Table(Of Object)

   Dim tableType As Type = GetType(Table(Of )).MakeGenericType(t)

   table = FormatterServices.GetUninitializedObject(tableType)

End sub 

So in short - is there a way of changing a variable type at runtime? (or a better way of doing what I'm doing)

Thanks in advance.

James

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about reflection