Achilles
Achilles

Reputation: 11299

How do I determine the type a generic container is storing?

I'm interrogating an object properties looking for ones that are of a specific type TypeOfInterest or are generics storing object of TypeOfInterest . How can I tell for a List(of T) that T is TypeOfInterest ?

Upvotes: 2

Views: 59

Answers (3)

NateTheGreat
NateTheGreat

Reputation: 2305

This page shows how: http://msdn.microsoft.com/en-us/library/b8ytshk6(v=vs.96).aspx

In short, you get the Type value from your List and then use the GetGenericArguments method.

Upvotes: 1

Bogdan Verbenets
Bogdan Verbenets

Reputation: 26946

YourList is List<TypeOfInterest>

If you want to make sure that the list contains only variables of that class, then you'll need to check each one of them.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81660

Do you mean

Console.WriteLine(typeof(List<string>).GetGenericArguments()[0] == typeof(string));

Upvotes: 1

Related Questions