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

后端 未结 2 565
野性不改
野性不改 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:04
    from s in series
    where s.GetType().GetGenericTypeDefinition()==typeof(ITraceSeries<>)
    select s;
    
    0 讨论(0)
  • 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<>))));
    
    0 讨论(0)
提交回复
热议问题