Is there a way to get the type of the objects stored in a generic list?

You could try typeof(List). GetGenericArguments()0 This works with an empty array, while your version does not UPDATE: On an instance use instance.GetType(). GetGenericArguments()0.

You could try typeof(List). GetGenericArguments()0 This works with an empty array, while your version does not. UPDATE: On an instance use instance.GetType().

GetGenericArguments()0.

1 @Artur, a stupid question! Thank you so much. – Phil Gan Jan 20 at 12:31 4 Not stupid, but easy ;).

I'm supriesed how many people would risk a IndexOutOfRangeException on this one. – Michael Stoll Jan 20 at 12:37.

Is not hackish at all. That is why the GetType() method exits.To obtain the type of the object.

1 Do it! No hack! – Martin Buberl Jan 20 at 12:15 3 I disagree.

What if the list is empty? – Phil Gan Jan 20 at 12:15 1 @Phil He explicitly said "suppose list is populated" – Liviu M. Jan 20 at 12:17 1 @Liviu, yes I know because I wrote the original post.

– Phil Gan Jan 20 at 12:25 @Phil Oh, indeed :)) lOL – Liviu M. Jan 20 at 12:27.

You can use myList.GetType(). GetGenericArguments() This returns an array of all the types specified in the declaration of the object.

It is hackish because if the list isn't populated, you can't get an answer. You'll need to reflect against the Type: List mylist = new List(); Type listType = mylist.GetType(); Type genericType = listType. GetGenericArguments()0.

You can just indent each line of a code block by 4 spaces (or highlight it and click the toolbar button). You don't have to use that / nonsense here on SO. :-) – Cody Gray Jan 20 at 12:22.

Do it as you said. It is not hackish. You can also call GetType() directly on your list and use it to look at type of its its T.

You could also do listInstance0 is SomeTypeIExpectThisToBe if you are expecting a type and want to do something because of that.

You should have access to the type parameter, so you can use typeof: void ProcessList( List listInstance) { Type type = typeof(T); }.

I suppose if the list is populated I could take listInstance0.GetType() but that seems a bit hackish. For context, I want to populate a DataTable with columns based on the Properties of an object. Where an object property is a generic list I want to add a column for each property of the object stored by the list.

I'll flatten the data structure to fit into a DataRow later. The reason I don't want to use the type of the first object in the list is because it's not guaranteed that every instance will have the list populated. Some will and some won't, but I'll still need all the columns ahead of time.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions