473,396 Members | 1,893 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,396 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 2465
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.