using Generics dynamically (?)

G

Gazarsgo

This seems to be a bit of a contradiction, but how can I construct a
Generic class using a System.Type variable that is assigned at run
time? Is there some parallel to the Generics concept that extends to
having strictly-typed classes at run-time?

I would gladly give up the compile-time errors if I could get rid of
all these CType()s :)
 
H

Herfried K. Wagner [MVP]

Gazarsgo said:
This seems to be a bit of a contradiction, but how can I construct a
Generic class using a System.Type variable that is assigned at run
time?

Maybe you are looking for 'Type.MakeGenericType' and
'Activator.CreateInstance'?
Is there some parallel to the Generics concept that extends to
having strictly-typed classes at run-time?

Strict typing is a compile-time feature.
 
J

Jay B. Harlow [MVP - Outlook]

Gazarsgo,
In addition to the other comments.

| Is there some parallel to the Generics concept that extends to
| having strictly-typed classes at run-time?
That is the major point behind Generics. You have strictly-typed classes at
compile & runtime

For example:

' create a loosely typed collection of integers in .NET 1.x
Dim aList As New ArrayList
aList.Add(1)
aList.Add(2)
aList.Add("Silly Me") ' allowed as its loosely typed!

' create a strictly-typed collection of integers in .NET 2.0
Dim aList As New List(Of Integer)
aList.Add(1)
aList.Add(2)
aList.Add("Silly Me") ' Compile error as its strictly typed!


| This seems to be a bit of a contradiction, but how can I construct a
| Generic class using a System.Type variable that is assigned at run
| time?
You should be able to use Type.MakeGenericType & Activator.CreateInstance,
something like:

Dim aType As Type = GetType(String)

Dim theCollectionType As Type =
GetType(System.Collections.ObjectModel.Collection(Of ))

Dim myColType As Type = theCollectionType.MakeGenericType(aType)

Dim mycol As Object = Activator.CreateInstance(myColType)


HOWEVER: Without knowing the actual type at compile time, you will only be
able to use late binding, Reflection, & object variables with the created
type, plus
possibly a handful of interfaces.

| I would gladly give up the compile-time errors if I could get rid of
| all these CType()s :)
?? CType doesn't help with have a variable of System.Type per se. However
Generics should reduce or elimate the "need" for CType.

FWIW: This is a good FAQ on Generics:

http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dndotnet/html/Fundamentals.asp

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| This seems to be a bit of a contradiction, but how can I construct a
| Generic class using a System.Type variable that is assigned at run
| time? Is there some parallel to the Generics concept that extends to
| having strictly-typed classes at run-time?
|
| I would gladly give up the compile-time errors if I could get rid of
| all these CType()s :)
|
 
C

Cor Ligthert [MVP]

Gazargsgo,

Never tried and never will, however are you not just looking at "object".

Cor
 
G

Gazarsgo

Cor said:
Never tried and never will, however are you not just looking at "object".

I am trying to implement an alternative to Object references using
Generics, so as to have my class functions return the specific type
rather than an Object reference, and to avoid boxing where possible.

Strict typing is a compile-time feature.

Maybe this is my own ignorance, but by "strict typing" I meant to refer
to type safety, whether implemented via static typing at compile time
or dynamic typing at runtime.


It looks like Type.MakeGenericType and Activator.CreateInstance are
exactly what I was looking for, thanks for the extended code snippet
Jay.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top