網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C#快速實(shí)現(xiàn)IList非泛型類(lèi)接口的自定義類(lèi)作為數(shù)據(jù)源_C#教程
作者:河西石頭 ? 更新時(shí)間: 2023-04-07 編程語(yǔ)言使用可以綁定數(shù)據(jù)源的控件我們需要有實(shí)現(xiàn)了IList接口的類(lèi)作為數(shù)據(jù)源,我們有很多的方法,比如使用ArrayList或者List的泛型類(lèi)都是很方便的,或者不怕麻煩的索性直接上DataTable。
但我們也許想實(shí)現(xiàn)一個(gè)專(zhuān)用于某個(gè)自己定義的對(duì)象的list類(lèi),這樣其他類(lèi)想錯(cuò)誤的加入這個(gè)list都不可能了。
一、利用VS的修補(bǔ)程序快速繼承IList
假定我有一個(gè)Creature的類(lèi),如果我們直接在上面加上接口的繼承,則會(huì)出現(xiàn)報(bào)錯(cuò)提示,如下圖:
說(shuō)明,這些接口成員都是必須實(shí)現(xiàn)的。
我們來(lái)一一實(shí)現(xiàn),其實(shí)也不必要,因?yàn)槲覀冎皇墙栌盟慕涌谧孋reature類(lèi)成為一個(gè)可以充當(dāng)數(shù)據(jù)源DataSource的類(lèi)。
我們點(diǎn)擊最下面的顯示可能的修補(bǔ)程序(Alt+Enter即可),然后點(diǎn)預(yù)覽,可以根據(jù)自己的需要修改。
如果不需要特別的修改,基本直接應(yīng)用即可,只是不能應(yīng)用到數(shù)據(jù)源綁定上。表面上看這樣這個(gè)類(lèi)就實(shí)現(xiàn)了IList接口了,但要用于數(shù)據(jù)源綁定就必須實(shí)現(xiàn)我所列出的5個(gè)成員,否則還是不能做為數(shù)據(jù)源給控件使用。
二、實(shí)現(xiàn)必須的成員
#region 做數(shù)據(jù)綁定必須實(shí)現(xiàn)的成員
/// <summary>
/// 添加元素必須的方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int Add(object? value)
{
list.Add(value);
return list.Count;
//throw new NotImplementedException();
}
public int Count { get { return list.Count; } }
public object? this[int index] {
get { return list[index]; }
set { list[index] = value; }
}
/// <summary>
/// 如果要作為DataGridView的數(shù)據(jù)源,必須實(shí)現(xiàn)這個(gè)屬性
/// </summary>
public bool IsReadOnly { get { return false;}
}
/// <summary>
/// 迭代必須的方法
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public IEnumerator GetEnumerator()
{
return list.GetEnumerator();
//throw new NotImplementedException();
}
#endregion
我們來(lái)看看效果:
這里我們測(cè)試了三種綁定數(shù)據(jù)源的控件,分別是ListBox,ComboBox,DataGridView ,沒(méi)有發(fā)現(xiàn)任何問(wèn)題,是不是特別的容易!
原文鏈接:https://haigear.blog.csdn.net/article/details/128771648
相關(guān)推薦
- 2023-05-19 python?txt中的文件,逐行讀取并且每行賦值給變量問(wèn)題_python
- 2022-03-24 C語(yǔ)言make和Makefile介紹及使用_C 語(yǔ)言
- 2022-09-26 Go并發(fā)同步Mutex典型易錯(cuò)使用場(chǎng)景_Golang
- 2022-06-08 記一次網(wǎng)站全站http升級(jí)為https的過(guò)程,websocket : ws升級(jí)為wss遇到的問(wèn)題等
- 2023-01-02 Python創(chuàng)建相同值數(shù)組/列表的兩種方法_python
- 2023-03-20 Linq利用Distinct去除重復(fù)項(xiàng)問(wèn)題(可自己指定)_C#教程
- 2022-11-17 使用Python中Tkinter模塊的Treeview?組件顯示ini文件操作_python
- 2022-04-27 jquery+css實(shí)現(xiàn)移動(dòng)端元素拖動(dòng)排序_jquery
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支