473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Use a System.Type in Generics?

Is this something you are not allowed to do?

I basically want to create an instance of a generic class where the
type T is not know at compile time.

e.g.

dim s as System.Type = GetMyType()
dim o as MyObj(Of s)
I have not found a way to do this, so I am guessing it is not possible.

Jun 5 '06 #1
4 1276
"cmay" <cm**@walshgroup.com> schrieb:
I basically want to create an instance of a generic class where the
type T is not know at compile time.

e.g.

dim s as System.Type = GetMyType()
dim o as MyObj(Of s)


\\\
Dim GenericType As Type = GetType(List(Of ))
Dim ParameterTypes() As Type = {GetType(Integer)}
Dim o As Object = _
Activator.CreateInstance( _
GenericType.MakeGenericType(ParameterTypes) _
)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jun 5 '06 #2
Thanks HKW!
Herfried K. Wagner [MVP] wrote:
"cmay" <cm**@walshgroup.com> schrieb:
I basically want to create an instance of a generic class where the
type T is not know at compile time.

e.g.

dim s as System.Type = GetMyType()
dim o as MyObj(Of s)


\\\
Dim GenericType As Type = GetType(List(Of ))
Dim ParameterTypes() As Type = {GetType(Integer)}
Dim o As Object = _
Activator.CreateInstance( _
GenericType.MakeGenericType(ParameterTypes) _
)
///

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


Jun 5 '06 #3
Herfried,

I am curious how I can use this Generic List of a typed class, while I don't
know the members of the class?

Can you give me an example?

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:Of**************@TK2MSFTNGP02.phx.gbl...
"cmay" <cm**@walshgroup.com> schrieb:
I basically want to create an instance of a generic class where the
type T is not know at compile time.

e.g.

dim s as System.Type = GetMyType()
dim o as MyObj(Of s)


\\\
Dim GenericType As Type = GetType(List(Of ))
Dim ParameterTypes() As Type = {GetType(Integer)}
Dim o As Object = _
Activator.CreateInstance( _
GenericType.MakeGenericType(ParameterTypes) _
)
///

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

Jun 6 '06 #4
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
I am curious how I can use this Generic List of a typed class, while I
don't know the members of the class?


The problem is that you can only do that using reflection because 'List(Of
X)' is not a 'List(Of )'. Thus I do not think that the solution I posted
makes much sense because generics are mainly a compile-time feature and the
compile-time advantages are lost when using reflection.

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

Jun 6 '06 #5

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

Similar topics

1
by: Helium | last post by:
Is there any need for "System.Collections.Queue" any more? OK, .Net made the mistake to start without generics, but that is fixed now. Languages without support for generics could use...
1
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
6
by: Mark Rae | last post by:
Hi, I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses ActiveDirectory a great deal, and I'm trying to use the new System.Collections.Generic namespace where possible,...
2
by: Ravi Shekhar | last post by:
Hello, So I'm doing some mathematical modeling and it turns out I need distinct imaginary and complex types to carry out complex contour integration. I want to have it in all three precisions...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
62
by: Laurent Deniau | last post by:
I just put the draft of my paper on the web: http://cern.ch/laurent.deniau/html/cos-oopsla07-draft.pdf I would be interested by any feedback from C programmers (with little OO knowledge) to...
4
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.