Test whether an object implements a generic interface for any generic type

前端 未结 1 1069
一向
一向 2021-01-18 15:12

I want to test an object to see if it implements IDictionary but I don\'t care what TKey and TValue are.

I

相关标签:
1条回答
  • 2021-01-18 15:21

    How about something like

    return type.GetInterfaces()
               .Where(t => t.IsGenericType)
               .Select(t => t.GetGenericTypeDefinition())
               .Any(t => t.Equals(typeof(IDictionary<,>)));
    

    which I'm sure that you can easily generalize for any generic type definition.

    0 讨论(0)
提交回复
热议问题