Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:13:40+00:00 2026-05-11T10:13:40+00:00

how is it possible to know whether an object implements an indexer?, I need

  • 0

how is it possible to know whether an object implements an indexer?, I need to share a logic for a DataRow and a IDataReader, but they don’t share any interface.

I tried also with generics but don’t know what restriction should I put on the where clause.

public class Indexer {     // myObject should be a DataRow or a IDataReader     private object myObject;     public object MyObject {         get { return myObject; }         set { myObject = value; }     }     // won't compile, myObject has no indexer     public object this[int index] {         get { return myObject[index]; }         set { myObject[index] = value; }     }     public Indexer(object myObject) {         this.myObject = myObject;     } }  public class Caller {     void Call() {         DataRow row = null;         IDataReader reader = null;         var ind1 = new Indexer(row);         var ind2 = new Indexer(reader);         var val1 = ind1[0];         var val2 = ind1[0];     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:13:41+00:00Added an answer on May 11, 2026 at 10:13 am

    You’d need to declare an interface with an indexer property, use that interface as the constraint, and the type argument class would need to implement that interface in order to satisfy the constraint.

    As you don’t control the classes you want to use, that wouldn’t work.

    An alternative is to make the Indexer class take the get/set operations as separate parameters:

    public class Indexer {      private Func<int, object> getter;             private Action<int, object> setter;      public object this[int index]      {         get { return getter(index); }         set { setter(index, value); }     }      public Indexer(Func<int, object> g, Action<int, object> s)      {         getter = g;         setter = s;     } }  public static class IndexerExtensions {     public static Indexer ToIndexer(this DataRow row)     {         return new Indexer(n => row[n], (n, v) => row[n] = v);     }      public static Indexer ToIndexer(this IDataReader row)     {         return new Indexer(n => row[n], (n, v) => row[n] = v);     } } 

    You could then do this:

    DataRow row = null; IDataReader reader = null; var ind1 = row.ToIndexer(); var ind2 = reader.ToIndexer(); var val1 = ind1[0]; var val2 = ind1[0]; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whether this is possible I don't know, but it would mighty useful! I have
I don't know whether it's possible, but I'd like to be able to write
To be more precise, I need to know whether (and if possible, how) I
I am new to android application. I need to know whether is it possible
Is it possible to know whether a Java class has been loaded, without attempting
I would like to know whether it is possible to force LWP::UserAgent to accept
Does anyone know if it is possible to detect whether the browser has closed
Does anyone know if it is possible to reliably determine (programattically C/C++...) whether or
Does anyone know whether it is possible to play a video that was synched
I would like to know whether its possible to open Open word 2003 (doc)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.