2

For some reason this code:

MethodInfo iDataReaderIndexerProperty = typeof(IDataReader).GetProperties()
    .Single(property => property.GetIndexParameters().Length > 0)
    .GetGetMethod();

fails. If I replace IDataReader with IMyInterface defined as:

interface IMyInterface
{
    String this[Int32 index] { get; }
}

it works fine. How does IDataReader define it's indexer?

3
  • 2
    Actually, that indexer comes from IDataRecord, not IDataReader Jun 17, 2013 at 19:30
  • @MarcGravell Spot on. That calls for an answer :)
    – nawfal
    Jun 17, 2013 at 19:31
  • Win. This is the correct answer. If you don't mind, please post that below and I'll go ahead and mark it as correct. Jun 17, 2013 at 19:32

2 Answers 2

6

That indexer is defined on IDataRecord, not IDataReader; so you need to query from typeof(IDataRecord), and use Servy's observation that there are multiple overloads (string vs int).

6

There are two indexers, one that takes an int an one that takes a string. Since you're using Single and there are two items that match, it will throw an exception.

You have several options:

  1. use First to get one of the two
  2. use Where and handle both indexers as a sequence
  3. add an additional constraint to ensure you just get the indexer that takes an int, and not the one that takes a string.
1
  • That's what I thought at first, but I get Sequence contains no matching element. Jun 17, 2013 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.