c# – 检测类型是否实现ICollection

c# – 检测类型是否实现ICollection,第1张

概述参见英文答案 > How to determine if a type implements a specific generic interface type                                    11个 我试图检查一个类型是否实现了通用的ICollection< T> interface,因为这是我的任何泛型集合的基本接口. 以下代码不起作用 GetType(I 参见英文答案 > How to determine if a type implements a specific generic interface type11个
我试图检查一个类型是否实现了通用的ICollection< T> interface,因为这是我的任何泛型集合的基本接口.

以下代码不起作用

GetType(ICollection(Of)).IsAssignableFrom(    objValue.GetType().GetGenericTypeDeFinition())

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

解决方法
CustomCollection c = new CustomCollection();bool implementICollection = c.GetType().GetInterfaces()                            .Any(x => x.IsGenericType &&                            x.GetGenericTypeDeFinition() == typeof(ICollection<>));
总结

以上是内存溢出为你收集整理的c# – 检测类型是否实现ICollection全部内容,希望文章能够帮你解决c# – 检测类型是否实现ICollection所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1241694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存