471,357 Members | 1,085 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to dynamically create generic type from other

I have some code like

if (val.GetType().Name == typeof(List<>).Name)
{

}

which lets me determine if val is a generic list(I've already taken care of
the non-generic part but I'd rather be able to compare them like if (val is
List<>) but this obviously doesn't work. (and I can't code it for every
parameter)

But now I need to create a new type reference from val so I can use its
methods.

in the non-generic version I have

List l = new List() because I know its a list.

In the generic version I can't do this cause I don't know the generic
parameter at compile time. Something like

List<objectl = (List<object>)val;

doesn't even work which might be ok if it did.

The code I'm using is independent of the generic parameter so it doesn't
really matter what it is(casting them to objects isn't an issue for me as
long as the type is bound to it).
Any ideas?

Thanks,
Jon
Apr 4 '08 #1
5 3524
I'd go with something like: [snip]
Note that that neither this nor your code copes with types derived from
List<T>.
OK; kinda spooky... one day I'm going to have an original thought!
Apr 4 '08 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Jon Slaughter <Jo***********@Hotmail.comwrote:
>I have some code like

if (val.GetType().Name == typeof(List<>).Name)
{

}

which lets me determine if val is a generic list

That's not really ideal - I don't generally like using the names of
types as points of comparison. I'd go with something like:

Type t = val.GetType();

bool isList = (t.IsGenericType &&
t.GetGenericTypeDefinition()==typeof(List<>));

Note that that neither this nor your code copes with types derived from
List<T>.
>But now I need to create a new type reference from val so I can use its
methods.

in the non-generic version I have

List l = new List() because I know its a list.

In the generic version I can't do this cause I don't know the generic
parameter at compile time. Something like

List<objectl = (List<object>)val;

doesn't even work which might be ok if it did.

I'm glad it doesn't, because otherwise you could end up adding plain
objects to a non-object List.
well, its just a reference though. It could throw and exception if you tried
to actually add them. I guess though theres no run-time way to know.
What are you actually trying to *do* with this? It's not clear what the
motivation is, so it's hard to suggest an alternative.
I'll post the code in a replay to the OP.
Apr 4 '08 #3
BTW, one way would be to create a generic list of type object and then
convert it. The objects will be of the right type but I need to somehow
convert it.

e.g., I can read ints into a List<objectwhere each object is truely an
int... but then I need to convert List<objectto List<int>
dynamically(e.g., I don't know its an int, it could be a float or anything
else)

(heh, I'm not sure if I'm making sense here because you probably don't see
why I don't know the type parameter at compile time. I do know it but
basically the routine is suppose to work with any type parameter(or most))

Apr 4 '08 #4
Just found this on the net and seems to be exactly what I was looking for,

Type genericType = typeof(List<>);

Type constructedType = genericType.MakeGenericType(typeof(Int32));

Hopefully I won't have any problems with createinstance now.
Apr 4 '08 #5

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:jJ*************@newssvr29.news.prodigy.net...
Just found this on the net and seems to be exactly what I was looking for,

Type genericType = typeof(List<>);

Type constructedType = genericType.MakeGenericType(typeof(Int32));

Ok, that works. I'm now able to read into generic collections.
Apr 4 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by pmclinn | last post: by
4 posts views Thread by puzzlecracker | last post: by
4 posts views Thread by Gazarsgo | last post: by
9 posts views Thread by netasp | last post: by
15 posts views Thread by =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post: by
reply views Thread by XIAOLAOHU | 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.