471,349 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by michael.f.ellis | last post: by
4 posts views Thread by Charles Churchill | last post: by
6 posts views Thread by Jakob Bieling | last post: by
6 posts views Thread by Jorge Varas | last post: by
3 posts views Thread by BombDrop | last post: by
12 posts views Thread by Howard | last post: by
5 posts views Thread by Jon Slaughter | last post: by
6 posts views Thread by Andrus | 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.