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

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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 said:
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]
- (e-mail address removed)


Scottie_do said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Scottie_do said:
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 said:
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]
- (e-mail address removed)


Scottie_do said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top