C#實(shí)現(xiàn)定義一個通用返回值
場景
很多情況下,我們在使用函數(shù)的時候,需要return多個返回值,比如說需要獲取處理的狀態(tài)以及信息、結(jié)果集等。最古老的時候,有用ref或者out來處理這個情況,但是需要定義處理等操作之類的,而且書寫也不是很舒服,感覺就是不太完美;后來有了dynamic后,覺得也是個不錯的選擇,可惜也有很多局限性,比如:匿名類無法序列化、返回結(jié)果不夠直觀等。再然后就寫一個結(jié)構(gòu)化的對象來接收,但是如果每個方法都定義一個對象去接收的話,想必也會很麻煩;
需求
所以,綜上場景所述,我們可以寫一個比較通用的返回值對象,然后使用泛型來傳遞需要return的數(shù)據(jù)。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
Visual Studio 2013
實(shí)現(xiàn)代碼
[Serializable] public class ReturnResult { public ReturnResult(Result _result, string _msg) { this.result = _result; this.msg = _result == Result.success ? "操作成功!" + _msg : "操作失敗!" + _msg; } public ReturnResult(Result _result) : this(_result, "") { } public Result result { get; set; } public string msg { get; set; } } [Serializable] public class ReturnResult<T> : ReturnResult { public ReturnResult(T _data) : base(Result.success) { this.data = _data; } public ReturnResult(Result _result, string _msg) : base(_result, _msg) { } public ReturnResult(Result _result, string _msg, T _data) : base(_result, _msg) { this.data = _data; } public T data { get; set; } } public enum Result { error = 0, success = 1 }
public ReturnResult<string> GetMsg() { return new ReturnResult<string>("msg"); } public ReturnResult<int> GetCode() { return new ReturnResult<int>(10); } public ReturnResult<Student> GetInfo() { Student student = new Student { id = 1, name = "張三" }; return new ReturnResult<Student>(student); } public class Student { public int id { get; set; } public string name { get; set; } } private void button1_Click(object sender, EventArgs e) { var result = GetCode(); if (result.result == Result.success) { MessageBox.Show(result.msg + result.data); } } private void button2_Click(object sender, EventArgs e) { var result = GetMsg(); if (result.result == Result.success) { MessageBox.Show(result.msg + result.data); } } private void button3_Click(object sender, EventArgs e) { var result = GetInfo(); if (result.result == Result.success) { MessageBox.Show(result.msg + Newtonsoft.Json.JsonConvert.SerializeObject(result.data)); } }
實(shí)現(xiàn)效果
代碼解析:挺簡單的,也沒啥可解釋的。
到此這篇關(guān)于C#實(shí)現(xiàn)定義一個通用返回值的文章就介紹到這了,更多相關(guān)C#通用返回值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條顯示效果,感興趣的小伙伴可以嘗試一下2022-06-06C#實(shí)現(xiàn)多線程的幾種方式小結(jié)
多線程是C#中一個重要的概念,多線程指的是在同一進(jìn)程中同時運(yùn)行多個線程的機(jī)制,多線程適用于需要提高系統(tǒng)并發(fā)性、吞吐量和響應(yīng)速度的場景,可以充分利用多核處理器和系統(tǒng)資源,提高應(yīng)用程序的性能和效率,本文介紹了C#實(shí)現(xiàn)多線程的幾種方式,需要的朋友可以參考下2024-07-07.NET/C# 使用Stopwatch測量運(yùn)行時間
這篇文章主要介紹了.NET/C# 使用Stopwatch測量運(yùn)行時間,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01VS2019屬性管理器沒有Microsoft.Cpp.x64.user的解決辦法
這篇文章主要介紹了VS2019屬性管理器沒有Microsoft.Cpp.x64.user的解決辦法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03C# SendInput 模擬鼠標(biāo)操作的實(shí)現(xiàn)方法
C# SendInput 模擬鼠標(biāo)操作的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04C#?操作Windows注冊表的實(shí)現(xiàn)方法
本文主要介紹了C#?操作Windows注冊表的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03