How to filter to all variants of a generic type using OfType<>

后端 未结 2 580
野性不改
野性不改 2021-01-12 18:27

I want to filter objects in a List using their type, using OfType<>. My problem is, that some objects are of a generic interf

2条回答
  •  难免孤独
    2021-01-12 19:22

    You can use reflection to achieve it:

    var filteredList = myList.Where(
        x => x.GetType()
              .GetInterfaces()
              .Any(i => i.IsGenericType && (i.GetGenericTypeDefinition() == typeof(ITraceSeries<>))));
    

提交回复
热议问题