Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Type of T of the generic List<T> object [duplicate]

Possible Duplicate:
Reflection - Getting the generic parameters from a System.Type instance
Get actual type of T in a generic List<T>

I'm looping through a PropertyInfo-List from a Class. In this class, I'm interested in the different Lists - for example List<int>, List<string>, List<Book>, aso.

But I don't know how I get the Type of the List-Objects. I just get something like

System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]].

I could take this information apart and create a Type out of this information, but I hope there's a smoother way?

like image 907
Richard Avatar asked Dec 20 '25 04:12

Richard


1 Answers

Simple:

Type type = list.GetType().GetGenericArguments()[0];
// The first argument will be the `T` inside `List<T>`... so the list type ;)
like image 94
Erre Efe Avatar answered Dec 22 '25 19:12

Erre Efe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!