C# 中自定义泛型类的示例

2021-09-08 12:11

阅读:502

标签:orm   泛型类   collect   pre   绑定   index   自定义   姓名   实体   一、自定义泛型类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpOOP_Lesson4 { /// /// 下拉框类 /// public class CompBox { public string ItemText { get; set; } public T ItemValue { get; set; } } } 二、实体类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpOOP_Lesson4 { /// /// 程序员类 /// public class SE { /// /// 工号 /// public string ID { get; set; } /// /// 姓名 /// public string Name { get; set; } } }   三、应用 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CSharpOOP_Lesson4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// /// 初始化 /// /// /// private void Form1_Load(object sender, EventArgs e) { List list = new List(); SE se = new SE(); se.Name = "jack"; se.ID = "10000"; // 下拉框对象 CompBox comp = new CompBox(); comp.ItemText = "jack"; comp.ItemValue = se; // 下拉框对象1 CompBox comp1 = new CompBox(); comp1.ItemText = "mary"; comp1.ItemValue = se; list.Add(comp); list.Add(comp1); // 绑定集合 comboBox1.DataSource = list; // 显示 comboBox1.DisplayMember = "ItemText"; // 值 comboBox1.ValueMember = "ItemValue"; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // 选择项 var se = (CompBox)this.comboBox1.SelectedItem; } } }  C# 中自定义泛型类的示例标签:orm   泛型类   collect   pre   绑定   index   自定义   姓名   实体   原文地址:http://www.cnblogs.com/xiaobudong/p/7447415.html


评论


亲,登录后才可以留言!