Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between instances of List<T>

Is there any way to tell via reflection that a generic list of Type A is related to a generic list of Type B? For example, I have a List<string> and a List<int>. How can I tell via reflection that both these types are 'instances' of List<T>. I think I'm having a problem because List<T> isn't a real type. You can't do typeof(List<T>) for example. List<T> is a compiler trick I guess. So is there anyway to determine if two different types come from List<T>?

like image 223
BowserKingKoopa Avatar asked Jan 19 '10 06:01

BowserKingKoopa


1 Answers

Sure you can... List<> is actually what's called an "unbound generic type" meaning it has not been parameterized with a type. When the type argument is specified it's called a "bound generic type". A type which involves "raw" type parameters, like List<T> is an "open generic type", and one that involves only actual types, like List<int> is a "closed generic type". The only situation in which an unbound generic type may be used in C# is in the typeof operator. To access the unbound type, or a closed type you would do:

Type listOfT = typeof(List<>); // unbound type
Type listOfString = typeof(List<string>); // closed bound type
Type listOfInt32 = typeof(List<int>); // closed bound type

Assert.IsTrue(listOfString.IsGenericType);

Assert.AreEqual(typeof(string), listOfString.GetGenericTypeParameters()[0]);
Assert.AreEqual(typeof(List<>), listOfString.GetGenericTypeDefinition());

Type setOfString = typeof(HashSet<string>);

Assert.AreNotEqual(typeof(List<>), setOfString.GetGenericTypeDefinition());
like image 173
Josh Avatar answered Oct 04 '22 09:10

Josh