473,387 Members | 1,569 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,387 software developers and data experts.

Get element type of generic list

I have a method that I would like to be able to pass a generic list
(such as List<string>, List<int>, etc). I'm trying to figure out what
would be the best type of parameter to use to pass in the list and if
there is a way to find out what type of elements it is designed to
contain (even if it currently has no elements). How would I go about
doing this?
Jun 27 '08 #1
3 9474
Taurin <ta*******@gmail.comwrote:
I have a method that I would like to be able to pass a generic list
(such as List<string>, List<int>, etc). I'm trying to figure out what
would be the best type of parameter to use to pass in the list and if
there is a way to find out what type of elements it is designed to
contain (even if it currently has no elements). How would I go about
doing this?
public void Foo<T>(List<Tlist)
{
Console.WriteLine("T = {0}", typeof(T));
}

In other words, you can find out at execution time with "typeof".

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
Well, if you want to be able to pass a generic list, then I guess you
are talking about a generic method - i.e.

public void SomeMethod<T>(IList<Tlist) {...}

In which case, the answer is typeof(T). If you mean just "a
list" (IList, not IList<T>), then a lot of the standard framework code
is based around inspected the int-indexer. For example, the code below
resolves that the list is a string-list. In the absense of a non-
object int-indexer (i.e. no indexer, or it is just typed as object),
the framework code takes the first (zeroth) element.

There is one other special case worth mentioning; the data-binding
code also respects ITypedList, which tells you about properties of the
list items - this mainly applies to flexible runtime things like
DataTableView.

Marc

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
static class Program
{
static void Main()
{
IList list = new List<string>();
PropertyInfo prop =
list.GetType().GetProperty("Item", new Type[]
{ typeof(int) });
if (prop != null)
{
Console.WriteLine(prop.PropertyType.Name);
}
}
}

Jun 27 '08 #3
Thanks Jon that is EXACTLY what I needed. You're a big help and
quick.
public void Foo<T>(List<Tlist)
{
Console.WriteLine("T = {0}", typeof(T));

}
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
6
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1);...
6
by: Jorge Varas | last post by:
Hi all, Is it possible to have a collection of a generic type? for example: class a<twhere t : class, new { }
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
12
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
5
by: Jon Slaughter | last post by:
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...
6
by: Andrus | last post by:
I need to create method which returns my application object type as string : TypeName(typeof( List<int>)) should return "List<int>" or "System.Collections.Generic.List<int>" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.