我有一个某种类型(Type的对象)。需要检查此类型是否具有接口IList。
我怎么能做到这一点?
发布于 2009-08-04 07:10:23
假设您有一个类型为System.Type的对象type (我从OP中收集的内容),
Type type = ...;
typeof(IList).IsAssignableFrom(type)发布于 2009-08-04 07:06:48
您可以使用Type.GetInterface方法。
if (object.GetType().GetInterface("IList") != null)
{
// object implements IList
}发布于 2009-08-04 07:08:47
我认为最简单的方法是使用IsAssignableFrom。
因此,从您的示例可以看出:
Type customListType = new YourCustomListType().GetType();
if (typeof(IList).IsAssignableFrom(customListType))
{
//Will be true if "YourCustomListType : IList"
}https://stackoverflow.com/questions/1226023
复制相似问题