'is' pattern matching for generic types #4865
Replies: 2 comments 6 replies
-
What are you expecting Here is comparable code to the example: using System;
using System.Collections;
using System.Collections.Generic;
IList IList = new List<int>() { 3, 2, 1 };
Type List = IList.GetType();
if (List.IsGenericType && List.GetGenericTypeDefinition() == typeof(List<>))
{
List.GetMethod("Sort", Array.Empty<Type>()).Invoke(IList, default);
Type T = List.GetGenericArguments()[0];
if (T.IsAssignableTo(typeof(int)))
{
foreach (var o in IList)
{
Console.WriteLine(o);
}
}
} You should try to avoid using |
Beta Was this translation helpful? Give feedback.
-
This is a duplicate of #1225. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Would be very convenient to be able to test for a generic type using 'is', like this:
Obviously there are other ways to do this such as (a) using dynamic variables (b) factoring the method call (Sort, in this example) up into a nongeneric base class (c) using reflection. All of those approaches however are worse in one way or another.
Beta Was this translation helpful? Give feedback.
All reactions