我该如何检查一个类型是否实现了ICollection<T>
接口?
例如,假设我们有以下变量:
ICollection<object> list = new List<object>();
Type listType = list.GetType();
有没有办法判断listType
是否是一个通用的ICollection<>
?
我已经尝试过以下方法,但没有成功:
if(typeof(ICollection).IsAssignableFrom(listType))
// ...
if(typeof(ICollection<>).IsAssignableFrom(listType))
// ...
当然,我可以做以下事情:
if(typeof(ICollection<object>).IsAssignableFrom(listType))
// ...
但这只适用于 ICollection<object>
类型。如果我有一个 ICollection<string>
,它将失败。
ICollection<>
接口。 - Andrey Nasonov