473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using Generics dynamically (?)

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 :)

Jan 25 '06 #1
4 1545
"Gazarsgo" <ga******@gmail.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jan 25 '06 #2
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/de...ndamentals.asp

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Gazarsgo" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
| 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 :)
|
Jan 25 '06 #3
Gazargsgo,

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

Cor
Jan 25 '06 #4

Cor Ligthert [MVP] wrote:
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.
Herfried K. Wagner [MVP] wrote: 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.

Jan 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2533
by: SainTiss | last post by:
Hi, I want to dynamically find out the type of a template parameter of a generic class. E.g. suppose I've got a variable myCollection of type LinkedList<String>. Then I would like to be able to...
27
2437
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
3075
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
16
1802
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
8
3356
by: Chris Dunaway | last post by:
The next version of VS.Net (Whidbey) promises to add generics support to VB. I have a vague remembrance of Templates in C++, but I guess I need a refresher. What will generics allow us to do? ...
23
2515
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
5
2907
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
11
2447
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
1660
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.