You will need to declare an interface with the indexer property, use this interface as a constraint, and a class of type arguments will need to implement this interface to satisfy the constraint.
Since you do not control the classes you want to use, this will not work.
An alternative is to use the Indexer class for 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); } }
Then you can do the following:
DataRow row = null; IDataReader reader = null; var ind1 = row.ToIndexer(); var ind2 = reader.ToIndexer(); var val1 = ind1[0]; var val2 = ind1[0];
Daniel Earwicker Mar 26 '09 at 22:34 2009-03-26 22:34
source share