c# - 为什么通过反射找不到IList类型上的GetEnumerator()方法?

标签 c# .net reflection types ilist

这个代码当然是有效的。根据定义,IList 有一个 GetEnumerator() 方法。

System.Collections.IList list = new List<string>();
System.Collections.IEnumerator ienum = list.GetEnumerator();

但是,以下方法都无法找到名为 GetEnumerator 的 IList 类型的成员。

Type iListType= typeof(System.Collections.IList);
var member = iListType.GetMember("GetEnumerator");
var members = iListType.GetMembers().Where(x => x.Name == "GetEnumerator");
var method = iListType.GetMethod("GetEnumerator");
var methods = iListType.GetMethods().Where(x => x.Name == "GetEnumerator");

最佳答案

IList 类型上找不到 GetEnumerator,因为 IList 类型未声明 GetEnumeratorIList 扩展了声明它的 IEnumerable。因此,您需要更改代码以在 IEnumerable 类型上查找 GetEnumerator

Type type = typeof(System.Collections.IEnumerable);
var member = type.GetMember("GetEnumerator");

关于c# - 为什么通过反射找不到IList类型上的GetEnumerator()方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447163/

相关文章:

.net - WIF(使用 Thinktecture Identity Server)和双工 WCF channel

scala - Scala 2.10 中编译器生成的隐式值是什么?

c# - "System.Web.UI.MasterPage does not contain a definition..."ASP.NET 错误

c# - 来自类之间递归依赖的 System.StackOverflow 异常

c# - 匹配日期范围的正则表达式

c# - 为什么 .NET 创建新的子字符串而不是指向现有字符串?

c# - 如何实例化 HttpPostedFile

java - 使用反射从另一个 .java 文件获取变量

c# - 从 wpf datagrid 获取隐藏值

c# - 如何在javascript中使用.net资源文件