关于索引器和/或泛型的问题
如何知道一个对象是否实现了索引器?,我需要共享 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];
}
}
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要声明一个带有索引器属性的接口,使用该接口作为约束,并且类型参数类需要实现该接口才能满足约束。
由于您无法控制要使用的类,因此这是行不通的。
另一种方法是让
Indexer
类将 get/set 操作作为单独的参数:然后您可以这样做:
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:You could then do this:
您可以将 Indexer 设为一个抽象基类,其中包含两个子类,一个用于 DataRow,一个用于 IDataReader。
为了更容易使用,可以存在 2 个工厂方法,例如:
<代码>
他们可以为该类型创建一个特定的基类,并使用它自己的逻辑来处理索引。
这避免了为每个 get/set 调用不断检查类型的开销(以单个虚拟属性而不是标准属性为代价)。
You could make your Indexer an abstract base class, with two subclasses, one for DataRow, and one for IDataReader.
To make it easier to use, 2 factory methods could exist, such as:
They could create a specific base class for that type, with it's own logic for handling the indexing.
This avoids the overhead of checking types constantly for every get/set call (at the cost of a single virtual property instead of a standard property).
并对 set{} 使用相同的逻辑
and use the same logic for set{}