473,394 Members | 1,701 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,394 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 3624
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy | last post by:
Can anyone tell me if it is possible to dynamically create generic collections? simply put I want to be able to something like this : public object test(SomeBaseClass param1) { if...
12
by: Wardeaux | last post by:
All, Wanting to find a way to create web pages to add to my website without having to recompile the codebehind everytime I want to add a new one... Here's the deal: I have a web app that takes...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
4
by: puzzlecracker | last post by:
I have seen these terms used in Gang of 4 but could never thoroughly understand what it meant I perceive that C++ is both, but apparently it is only a latter. Can someone explain it? thx
4
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
1
by: Lorraine | last post by:
Hi all, I have test application written in C# from which I am trying to dynamically invoke a DLL. I have two dll's the only difference being one is written in C# and the other in VB.NET. The test...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
4
by: alan | last post by:
AFAIK a template parameter must be completely determinable by the compiler at compile time, is this correct? Currently my problem is, I have a class which contains a std::vector. The size of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.