如何编写一个函数采取任何物体与索引运算符 [英] how to write a function to take any object with an index operator

查看:97
本文介绍了如何编写一个函数采取任何物体与索引运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我已经在C的情况下问这个++在过去的(不能在我的问题!历史上找到它)和解决方案是使用一个模板函数。由于在编译的时候C ++模板的决心,它的工作原理。但对于C#,它没有

I think I have asked this in the context of C++ (Can't find it in my question history!!) in the past and the solution was to use a template function. As C++ template resolve at compile time, it works. But for C#, it doesn't.

public Hashtable ConvertToHashtable<T>(T source) where T has an index operator
{
    Hashtable table = new Hashtable();
    table["apple"] = source["apple"];

    return table;
}



目前的一个用法是将结果转换成OleDbReader到哈希表,但。我forseeing更多的源类型很快需要

One usage at the moment is to convert the result in OleDbReader to hashtable, but I am forseeing the need for more source types soon.

推荐答案

有没有通用的类型约束的在C#中运营商 - 这是在C#泛型的限制之一

There are no generic type constraints for operators in C# - it is one of the limitations of generics in C#.

这篇关于如何编写一个函数采取任何物体与索引运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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