개체에서 인덱서를 구현하는지 여부를 어떻게 알 수 있습니까? DataRow 및 IDataReader에 대한 논리를 공유해야하지만 인터페이스를 공유하지 않습니다.인덱서 및/또는 제네릭에 대한 질문
제네릭에서도 시도했지만 where 절에 어떤 제한을 두어야하는지 알지 못합니다.
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];
}
}
하지만 DataRow에서 내 인터페이스를 구현할 수 없습니다. –
업데이트 된 답변보기. –
나는 downvote하지 않았어 참고, 죄송합니다 –