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>
?
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With