I am attempting to use reflection to detect collections such as List<T>. Ultimately I need to operate without an instance available (i.e. via typeof only), and would like to detect collections beyond just List<T>, but to keep it simple here is a basic test case which fails:
Type type = (new List<string>()).GetType();
if (type.IsAssignableFrom(typeof(System.Collections.IEnumerable)))
{
Console.WriteLine("True.");
}
else Console.WriteLine("False.");
I have also tried IList and ICollection to no avail.
While troubleshooting I encountered the following discussion:
Why is List<int> not IEnumerable<ValueType>?
The OP of that discussion found his answer, that value types will not be detected by something like the above because covariance doesn’t work for them. However I am using string, a reference type and still not seeing covariance. What’s more curious (to me anyway), is that when I run the example code in the above discussion, I see a very different result:
System.Collections.Generic.List`1[AssignableListTest.Program+X] is most likely a list of ValueTypes or a string
System.Collections.Generic.List`1[System.String] is most likely a list of ValueTypes or a string
System.Collections.Generic.List`1[AssignableListTest.Program+Z] is most likely a list of ValueTypes or a string
System.Collections.Generic.List`1[System.Int32] is most likely a list of ValueTypes or a string
blah is most likely a list of ValueTypes or a string
1 is not a list
Leading me to believe I must have a configuration difference from that poster. I am using Visual Studio 2005 and in this case .NET 3.5, although I need compatibility back to 2.0 if possible. Actually, if I could get the result that poster had, it would be sufficient (identifying that IEnumerable is implemented), but when I use Type.IsAssignableFrom() in lieu of “is”, they all give “is not a list”.
You need to flip the check:
becomes
Everybody makes that mistake at least once. It is a misleading API.