views:

93

answers:

1

Is it possible to create a List or IEnumerable using GetType().

// If T is Type of Contact I want to Return List<Contact>

Test(typeof(Contact));//return List<Type>

    public static IEnumerable<T> Test<T>(T t)
    {
        return new List<T>();  //return List<Type>

    }
+3  A: 
 public static IList GetList(Type type)
 { 
    return (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
 }
Ani
Oh trust me, that latter GetList can be very useful at times.
Chris Charabaruk
@Chris: I edited that out, surprised you can still see it. :)
Ani
@Ani: Yeah, it was at revision 2 when I saw it, when it was still there. BTW, anyone can see older versions of edited questions and answers by clicking on the "edited X" datestamp, so...
Chris Charabaruk
+1 @Chris: It's incredible, but I didn't know of this feature.
Ani
@Ani: It took me quite a while to find it, myself. Or more accurately, to realize that it was available to all questions and answers, not just my own.
Chris Charabaruk