C# IDataReader如何定义它;s索引器?
出于某种原因,此代码:C# IDataReader如何定义它;s索引器?,c#,C#,出于某种原因,此代码: MethodInfo iDataReaderIndexerProperty = typeof(IDataReader).GetProperties() .Single(property => property.GetIndexParameters().Length > 0) .GetGetMethod(); 失败了。如果我将IDataReader替换为IMyInterface定义为: interface IMyInterface {
MethodInfo iDataReaderIndexerProperty = typeof(IDataReader).GetProperties()
.Single(property => property.GetIndexParameters().Length > 0)
.GetGetMethod();
失败了。如果我将IDataReader
替换为IMyInterface
定义为:
interface IMyInterface
{
String this[Int32 index] { get; }
}
它很好用。
IDataReader
如何定义它的索引器?有两个索引器,一个取整数,一个取字符串。由于您使用的是Single
,并且有两个匹配项,因此它将抛出异常
您有几个选择:
First
获取两个选项中的一个Where
并将两个索引器作为一个序列处理该索引器是在
IDataRecord
上定义的,而不是IDataReader
;因此,您需要从typeof(IDataRecord)
,和使用Servy的观察结果,即存在多个重载(string
vsint
)。这是我最初的想法,但我得到的序列不包含匹配元素
。实际上,该索引器来自IDataRecord,不IDataReader@MarcGravell完全正确这需要一个答案:)赢。这是正确的答案。如果你不介意的话,请把它贴在下面,我会把它标记为正确的。