471,327 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 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 2340
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Scottie_do | last post: by
1 post views Thread by Jinsong Liu | last post: by
reply views Thread by rosydwin | last post: by

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.