網站首頁 編程語言 正文
場景
很多情況下,我們在使用函數的時候,需要return多個返回值,比如說需要獲取處理的狀態以及信息、結果集等。最古老的時候,有用ref或者out來處理這個情況,但是需要定義處理等操作之類的,而且書寫也不是很舒服,感覺就是不太完美;后來有了dynamic后,覺得也是個不錯的選擇,可惜也有很多局限性,比如:匿名類無法序列化、返回結果不夠直觀等。再然后就寫一個結構化的對象來接收,但是如果每個方法都定義一個對象去接收的話,想必也會很麻煩;
需求
所以,綜上場景所述,我們可以寫一個比較通用的返回值對象,然后使用泛型來傳遞需要return的數據。
開發環境
.NET Framework版本:4.5
開發工具
?Visual Studio 2013
實現代碼
[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));
}
}
實現效果
代碼解析:挺簡單的,也沒啥可解釋的。
原文鏈接:https://mp.weixin.qq.com/s/rUvJrdFcBJtMBYPMjgOAOw
相關推薦
- 2022-07-11 UVM中uvm_config_db在sequence中的使用
- 2022-10-01 使用python+Flask實現日志在web網頁實時更新顯示_python
- 2021-12-12 C++多線程之互斥鎖與死鎖_C 語言
- 2023-12-18 Path環境變量點編輯無法展開
- 2022-08-21 Electron+React進行通信的方法_React
- 2022-12-29 react中將html字符串渲染到頁面的實現方式_React
- 2023-01-10 C#固定大小緩沖區及使用指針復制數據詳解_C#教程
- 2022-05-17 Mybatis中報錯:attempted to return null from a method
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支