使用未定义的Dictionary< Type,Type>作为方法的输入

编程入门 行业动态 更新时间:2024-10-25 06:26:08
本文介绍了使用未定义的Dictionary< Type,Type>作为方法的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试定义一种用于在Windows窗体上填充ComboBox控件的通用方法.我通常使用Dictionary通过将组合框绑定到数据源来填充组合框.由于只有少数几个,因此我想创建一种方法,可以仅对键,值对的任意组合进行调用.这是我的尝试:

I am trying to define a generic method for populating ComboBox controls on Windows Forms. I am usually using Dictionary to populate my combo boxes by binding them to the Data Source. Since I have a handful of those, I wanted to make a method that I can just call on any combination of a key, value pairs. Here's my attempt:

private void PopulateDropdown(ComboBox control, Dictionary<Type, Type> dict) { if (dict.Count > 0) { control.DataSource = new BindingSource(dict, null); control.DisplayMember = "Key"; control.ValueMember = "Value"; } }

我希望能够这样称呼它:

I would like to be able to call it like so:

PopulateDropdown(cbPrinters, this.inputData.Printers);其中, this.inputData.Printers是字典,键/值类型为字符串,字符串,但是我还有其他组合,例如字符串,整数或字符串,对象.

PopulateDropdown(cbPrinters, this.inputData.Printers); where this.inputData.Printers is a Dictionary with key/value types as string, string but I also have other combinations like string, int or string,object.

我遇到了无法将字符串,字符串转换为Type,Type的错误.我怎样才能解决这个问题?

I am getting an error that cannot convert string, string to Type, Type. How can I fix this?

推荐答案

您需要使用通用参数声明方法:

You need to declare your method with generic parameters:

private void PopulateDropdown<TKey,TValue>(ComboBox control, Dictionary<TKey, TValue> dict) { }

更多推荐

使用未定义的Dictionary&lt; Type,Type&gt;作为方法的输入

本文发布于:2023-11-09 06:08:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   方法   Dictionary   lt   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!