- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个方法,使用反射来判断给定类型是否实现 IList<T>
。例如:
IsGenericList(typeof(int)) // should return false
IsGenericList(typeof(ArrayList)) // should return false
IsGenericList(typeof(IList<int>)) // should return true
IsGenericList(typeof(List<int>)) // should return true
IsGenericList(typeof(ObservableCollection<int>)) // should return true
在我的使用中,我可以假设该类型始终是实例化的泛型类型(或者根本不是泛型的类型)。
不幸的是,这并不像应有的那么容易。显而易见的解决方案:
public bool IsGenericList(Type type)
{
return typeof(IList<>).IsAssignableFrom(type);
}
不起作用;它总是返回 false。显然是非实例化的泛型类型,例如 IList<>
不要按照我期望的方式实现 IsAssignableFrom:IList<>
不可从 List<T>
分配.
我也尝试过这个:
public bool IsGenericList(Type type)
{
if (!type.IsGenericType)
return false;
var genericTypeDefinition = type.GetGenericTypeDefinition();
return typeof(List<>).IsAssignableFrom(genericTypeDefinition);
}
即,转type
进入其非实例化泛型,例如 IList<int>
-> IList<>
,然后再次尝试 IsAssignableFrom。当类型被实例化时,这将返回 true IList<T>
如IList<int>
, IList<object>
等等。但对于实现 IList<T>
的类,它返回 false如List<int>
, ObservableCollection<double>
等等,显然IList<>
不可从 List<>
分配。再说一次,这不是我所期望的。
我该如何编写 IsGenericList 并使其像上面的示例一样工作?
最佳答案
事实上,您不能拥有泛型类型定义的实例。因此,IsAssignableFrom() 方法按预期工作。要实现您想要的目标,请执行以下操作:
public bool IsGenericList(Type type)
{
if (type == null) {
throw new ArgumentNullException("type");
}
foreach (Type @interface in type.GetInterfaces()) {
if (@interface.IsGenericType) {
if (@interface.GetGenericTypeDefinition() == typeof(ICollection<>)) {
// if needed, you can also return the type used as generic argument
return true;
}
}
}
return false;
}
出于好奇,你需要这个做什么?
关于.net - 如何判断 Type 是否实现了 IList<>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951536/
我正在尝试转换 IList>至 IList ,我的意思是有一个唯一的列表,其中包含第一个的所有元素(对象)。 public IList> PMTs { get
在 C# 中,如果我有一个 IList,但我不知道 IList 中对象的类型,我该如何创建一个 的副本>IList? 情况是这样的: 我有一个 CollectionEditor,它可以修改 IList
IList不继承 IList其中 IEnumerable继承IEnumerable . 如果out修饰符是大多数执行 IList 的唯一原因(例如 Collection , List )实现 ILis
尝试过: IList> matrix = new List()>(); 可是我做不到。我该怎么做?我需要一个字符串矩阵... 最佳答案 你需要: IList> matrix = new List>()
我有一个构建列表列表的方法。我想让返回类型使用通用 IList<> 接口(interface)来减少与下游具体 List<> 类型的耦合。但是,编译器在类型转换方面遇到了困难。 public ILis
我有一个 IList我想将此集合复制到 IList收藏 我试过这个: IList listAdminVAT = new AdministrationService(session).ListDecim
我有一个实现了 IList 的对象接口(interface),我想把它转换成IList或者 List ,我试过了 IList a=(IList)b; List a=(IList)b; IList a=
我似乎无法弄清楚为什么以下内容不起作用。 通过阅读,它似乎一定与 this 之类的东西有某种关联。 . public class Test { public void SomeFunction
我有几个类: class Vehicle { } class Car : Vehicle { } 我有一个派生类的列表: IList cars; 我想将列表转换为其基类,并尝试过: IList bas
List的定义在 .net 中显示它实现了各种接口(interface)。 public class List : IList, ICollection, IEnumerable, IList, IC
我想创建一个 ListCollectionView使用通用 IList但是构造函数得到一个非泛型 IList .如何将我的列表从 IList 转换为至 IList ? 如果有另一种方法可以创建 Lis
这个问题在这里已经有了答案: Remove items from one list in another (10 个答案) 关闭 8 年前。 很抱歉提出这样的基本问题,我是 LINQ 的新手,我正试
我正在实现 IListSource这需要一个方法 GetList()具有以下签名: IList GetList() 我正在使用 .NET Framework 2,我想返回一个实现 IList 的对象,
我有一个类(class)想要 IList , 但我有一个 Systems.Collection.IList , 来自 NHibernate 问题。 我想创建一个将其转换为 IList 的方法.我该怎么
我似乎找不到为我的 MVVM Xamarin 表单应用程序构建和使用数据的最佳实践或最佳机制。这可能是对如何解决问题的简单误解,但我不确定。 我曾尝试使用 IEnumerable、IList 和一个简
多年来,我们大部分时间都在使用泛型集合。有时我们确实需要任何东西的集合(好吧,通常只有一些不同的东西但没有公共(public)基类)。对于这种情况,我们可以使用 IList或通用 IList作为方法参
我有一个 IList目的。 Person 类有字段,FirstName , LastName等 我有一个函数需要 IList我想以与 IList 相同的顺序传入 FirstNames 列表目的。有没有
尝试这样转换 Products1 = (IList)basicProfile.Products2.Select(ToProductInfo) Products1 是一个 public IList Pr
我正在尝试创建一个列表列表,但在实例化列表时遇到了问题。 IList> allLists = List>(); 我遇到了这一行的编译错误。 最佳答案 你必须实例化一个 List的 IList , 不是
我有一个集合,它实现了一个扩展 IList 和 List 的接口(interface)。 public Interface IMySpecialCollection : IList, IList {
我是一名优秀的程序员,十分优秀!