473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ 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 1558
"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.MakeGener icType' and
'Activator.Crea teInstance'?
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("Sill y 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("Sill y 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.MakeGeneri cType & Activator.Creat eInstance,
something like:

Dim aType As Type = GetType(String)

Dim theCollectionTy pe As Type =
GetType(System. Collections.Obj ectModel.Collec tion(Of ))

Dim myColType As Type = theCollectionTy pe.MakeGenericT ype(aType)

Dim mycol As Object = Activator.Creat eInstance(myCol Type)
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.goo glegroups.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.MakeGeneri cType and Activator.Creat eInstance 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
2543
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 find out "String" when I only have myCollection. Looking at the 1.5 API, I would have expected this to work: myCollection.getClass().getTypeParameters().
27
2460
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 still no can do... Any info would be great!
2
3103
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
16
1837
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); } public class B {
8
3367
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? How do they make coding easier? Is there a resource that will give me a basic understanding of what generics are and how generics will be useful (in the context of VB.Net)? Thanks
23
2547
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 vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
5
2921
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 an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
11
2482
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>> _queries; where DLinqQuery is a generic class that takes a type parameter
1
1677
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
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.