Csharp/C#教程:如何编写一个函数来使用索引运算符获取任何对象分享


如何编写一个函数来使用索引运算符获取任何对象

我想我过去在C ++(在我的问题历史记录中找不到它)的上下文中问过这个问题,解决方法是使用模板函数。 当C ++模板在编译时解析时,它可以工作。 但是对于C#,它没有。

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

目前的一种用法是将OleDbReader中的结果转换为哈希表,但我预计很快就会需要更多源类型。

C#中的运算符没有generics类型约束 – 这是C#中generics的限制之一。

您可以使用界面:

 public interface IIndexable { T this[int index] { get; set; } T this[string key] { get; set; } } 

您的方法如下:

 public Hashtable ConvertToHashtable(T source) where T : IIndexable { Hashtable table = new Hashtable(); table["apple"] = source["apple"]; return table; } 

一个简单的来源是:

 public class Source : IIndexable { public Source this[int index] { get { // TODO: Implement } set { // TODO: Implement } } public Source this[string key] { get { // TODO: Implement } set { // TODO: Implement } } } 

一个简单的消费者是:

 public class Consumer{ public void Test(){ var source = new Source(); var hashtable = ConvertToHashtable(source); // you haven't to write: var hashtable = ConvertToHashtable(source); } } 

你能添加一个约束来指定type参数是IList吗?

 public Hashtable ConvertToHashtable(T source) where T : IList { Hashtable table = new Hashtable(); table["apple"] = source["apple"]; return table; } 

Item属性this[int index]不是运算符,它是包含类型的属性成员。 IList揭露了这一点。

如果运行时检查足够好,您可以使用reflection作为评论员之一,如下所示:

上述就是C#学习教程:如何编写一个函数来使用索引运算符获取任何对象分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 if (typeof (T).GetProperties().Any(property => property.Name.Equals("Item"))) 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/961988.html

(0)
上一篇 2021年11月26日
下一篇 2021年11月26日

精彩推荐