有关索引和/或泛型问题 [英] Question about indexers and/or generics

查看:150
本文介绍了有关索引和/或泛型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么可能知道对象是否实现索引器?我需要共享的一个DataRow和IDataReader的逻辑,但他们不共享任何接口。



我也试图与仿制药,但不知道我应该把什么限制的where子句。

 公共类索引{
// myObject的应该是一个DataRow或IDataReader的
私有对象myObject的;
公共对象为MyObject {
得到{myObject的; }
集合{myObject的=价值; }
}
//将无法编译,myObject的没有索引
公共对象本[INT指数] {
{返回myObject的[指数] }
集合{myObject的[指数] =值; }
}
公共索引(对象myObject的){
this.myObject = myObject的;
}
}

公共类来电{
void调用(){
的DataRow行= NULL;
的IDataReader读卡器= NULL;
变种IND1 =新的索引(行);
变种IND2 =新的索引器(读卡器);
变种val1中= IND1 [0];
变种val2的= IND1 [0];
}
}


解决方案

您倒是需要声明的接口与索引器属性,使用该接口作为约束,类型参数类需要实现,以满足约束接口。



当你不控制你想使用,这是行不通的类。



另一种方法是使索引班拿的get / set操作作为单独的参数:

 公共类索引{

私人Func键< INT,对象>吸收剂;
私人动作< INT,对象>二传手;

公共对象本[INT指数]
{
{返回吸气剂(指数); }
集合{setter方法(指数值); }
}

公共索引(Func键< INT,对象>克,动作< INT,对象> S)
{
吸气=克;
二传手= S;
}
}

公共静态类IndexerExtensions
{
公共静态索引ToIndexer(此的DataRow行)
{
返回新的索引(N =>行[N],(N,v)=>行[N] = v);
}

公共静态索引ToIndexer(此IDataReader的行)
{
返回新索引(N =>行[N],(N,V)= >行[N] = v);
}
}



然后,您可以这样做:

 的DataRow行= NULL; 
的IDataReader读卡器= NULL;
VAR IND1 = row.ToIndexer();
VAR IND2 = reader.ToIndexer();
变种val1中= IND1 [0];
变种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];
    }
}

解决方案

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];

这篇关于有关索引和/或泛型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆