检测类型是否实现ICollection <T>

np-*_*ard 7 .net c# vb.net reflection

我试图检查一个类型是否实现了通用ICollection <T>接口,因为这是我的任何通用集合的基本接口.

以下代码不起作用

GetType(ICollection(Of)).IsAssignableFrom(
    objValue.GetType().GetGenericTypeDefinition())
Run Code Online (Sandbox Code Playgroud)

检测类型是否实现通用接口的好方法是什么?

Sta*_* R. 26

CustomCollection c = new CustomCollection();

bool implementICollection = c.GetType().GetInterfaces()
                            .Any(x => x.IsGenericType &&
                            x.GetGenericTypeDefinition() == typeof(ICollection<>));
Run Code Online (Sandbox Code Playgroud)