472,801 Members | 1,155 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Can I specify a <Generic> type through a string?

I'm considering switching to C# and using VS2005, but I'd like to know if I
can have a list of values at runtime and then specify (at runtime) what it's
value type is.

For example, I would have this in my Multi-Dimensional Array named people[]
Then, I would then like to say:

new Person<People[index].ToString>();

Where ToString() would output a built in datatype or a struct/object that I
defined at compiletime.

If this can be done I'd be curious about the syntax in C++/CLR
Jan 23 '06 #1
3 2424
Scottie,

You can do this, but not with this nomenclature. You will have to use
reflection, getting the Type of the generic type without type parameters,
and then getting the type instance of the type you want to use for the type
parameter (using the string and loading the type).

Once you have this, you can call MakeGenericType on the Type instance
representing the generic type, and then it will return a type instance that
represents the fully constructed type.

The problem with this is that you have to actually use reflection to
call the methods, since you don't know what it is exposing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Scottie_do" <Sc*******@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
I'm considering switching to C# and using VS2005, but I'd like to know if
I
can have a list of values at runtime and then specify (at runtime) what
it's
value type is.

For example, I would have this in my Multi-Dimensional Array named
people[]
Then, I would then like to say:

new Person<People[index].ToString>();

Where ToString() would output a built in datatype or a struct/object that
I
defined at compiletime.

If this can be done I'd be curious about the syntax in C++/CLR

Jan 23 '06 #2
Thanks. Which would be more efficient (speed not size) if I were to use a
switch statement to assign the generic type at runtime, or reflection
(something I'm not too familiar with).
"Nicholas Paldino [.NET/C# MVP]" wrote:
Scottie,

You can do this, but not with this nomenclature. You will have to use
reflection, getting the Type of the generic type without type parameters,
and then getting the type instance of the type you want to use for the type
parameter (using the string and loading the type).

Once you have this, you can call MakeGenericType on the Type instance
representing the generic type, and then it will return a type instance that
represents the fully constructed type.

The problem with this is that you have to actually use reflection to
call the methods, since you don't know what it is exposing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Scottie_do" <Sc*******@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
I'm considering switching to C# and using VS2005, but I'd like to know if
I
can have a list of values at runtime and then specify (at runtime) what
it's
value type is.

For example, I would have this in my Multi-Dimensional Array named
people[]
Then, I would then like to say:

new Person<People[index].ToString>();

Where ToString() would output a built in datatype or a struct/object that
I
defined at compiletime.

If this can be done I'd be curious about the syntax in C++/CLR


Jan 23 '06 #3
Scottie,

I would say a switch statement. However, the drawback of that is that
you have to maintain the code, meaning, if you add new types, you have to
update your switch statement.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Scottie_do" <Sc*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Thanks. Which would be more efficient (speed not size) if I were to use a
switch statement to assign the generic type at runtime, or reflection
(something I'm not too familiar with).
"Nicholas Paldino [.NET/C# MVP]" wrote:
Scottie,

You can do this, but not with this nomenclature. You will have to
use
reflection, getting the Type of the generic type without type parameters,
and then getting the type instance of the type you want to use for the
type
parameter (using the string and loading the type).

Once you have this, you can call MakeGenericType on the Type instance
representing the generic type, and then it will return a type instance
that
represents the fully constructed type.

The problem with this is that you have to actually use reflection to
call the methods, since you don't know what it is exposing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Scottie_do" <Sc*******@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
> I'm considering switching to C# and using VS2005, but I'd like to know
> if
> I
> can have a list of values at runtime and then specify (at runtime) what
> it's
> value type is.
>
> For example, I would have this in my Multi-Dimensional Array named
> people[]
> Then, I would then like to say:
>
> new Person<People[index].ToString>();
>
> Where ToString() would output a built in datatype or a struct/object
> that
> I
> defined at compiletime.
>
> If this can be done I'd be curious about the syntax in C++/CLR


Jan 23 '06 #4

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

Similar topics

1
by: Dmitry Martynov | last post by:
What if I have some special constructors and do not have a default constructor. It seems to be very useful when I do not want to forget to initialize some fields properly. As I have understood...
2
by: Daniel Mori | last post by:
Hi, Im hoping someone can give me some advice. Im doing some development using VS Whidbey 2005 beta 1. Im about to implement some highly time critical code related to a managed collection of...
0
by: Scottie_do | last post by:
I'm considering switching to C# and using VS2005, but I'd like to know if I can have a list of values at runtime and then specify (at runtime) what it's value type is. For example, I would have...
1
by: Jinsong Liu | last post by:
Hello Group: I am playing with .NET 2.0 Generic. I have a CarsList<T> Generic class, Is it possible that I can control what T could be? I want to ensure the T can only be classes derived from a...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
5
by: Steve K. | last post by:
I know this code is garbage but it should give you an idea of what I'm after: <code> class Test { Type _t = null; public void SetSourceList<T>(Dictionary<string, TsourceList) { _t =...
1
by: DotNetNewbie | last post by:
Hi, In the source code for System.Web.MVC the generic class is defined as: public class ViewPage<TViewData: ViewPage I want to create my own custom ViewPage class, and inherit from the...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.