C#中返回值封装
在平时开发过程中常常需要取一个方法的返回值,BOSS写了一个返回值类,做个练习以备不时之需:
返回值支持泛型和非泛型 先贴上代码:
非泛型返回值类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.Serialization; 6 7 8 namespace WindowsFormsApplication31 9 { 10 [Serializable] 11 12 public class ReturnValue 13 { 14 /// <summary> 15 /// 状态:成功、失败 16 /// </summary> 17 18 public bool State 19 { 20 get; 21 protected set; 22 } 23 24 /// <summary> 25 /// 成功消息 26 /// </summary> 27 28 public string SuccessMessage 29 { 30 get; 31 protected set; 32 } 33 34 /// <summary> 35 /// 失败消息 36 /// </summary> 37 38 public string FailMessage 39 { 40 get; 41 protected set; 42 } 43 44 45 /// <summary> 46 /// 构造器 47 /// </summary> 48 public ReturnValue() 49 { 50 this.State = false; 51 this.SuccessMessage = string.Empty; 52 this.FailMessage = string.Empty; 53 } 54 55 /// <summary> 56 /// 设置状态 57 /// </summary> 58 /// <param name="state">状态</param> 59 /// <param name="message">消息</param> 60 private void Load(bool state, string message = "") 61 { 62 this.State = state; 63 if (state) 64 { 65 this.SuccessMessage = message; 66 } 67 else 68 { 69 this.FailMessage = message; 70 } 71 } 72 73 /// <summary> 74 /// 设置成功 75 /// </summary> 76 /// <param name="message">成功消息</param> 77 public void Success(string message = "") 78 { 79 this.Load(true, message); 80 } 81 82 /// <summary> 83 /// 设置失败 84 /// </summary> 85 /// <param name="message">失败消息</param> 86 public void Fail(string message = "") 87 { 88 this.Load(false, message); 89 } 90 } 91 }
泛型返回值类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.InteropServices; 6 using System.Runtime.Serialization; 7 8 9 10 11 namespace WindowsFormsApplication31 12 { 13 [Serializable] 14 15 public class ReturnValues<T> : ReturnValue 16 { 17 /// <summary> 18 /// 返回值 19 /// </summary> 20 21 public T Value 22 { 23 get; 24 set; 25 } 26 27 /// <summary> 28 /// 设置成功 29 /// </summary> 30 /// <param name="value">值</param> 31 /// <param name="message">成功消息</param> 32 public void Success(T value = default(T), string message = "") 33 { 34 this.State = true; 35 this.Value = value; 36 this.SuccessMessage = message; 37 } 38 } 39 }
实例:
1.演示窗体:
通过2个文本框输入信息
第一个开始返回文本验证信息
第二个开始按钮测试
2.关键代码:
非泛型:
#region 返回值(非泛型) /// <summary> /// 按钮1 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnVerifyEmpty_Click(object sender, EventArgs e) { ReturnValue rv = VerifyEmpty(); this.ReturnValueHelper(rv); } /// <summary> /// 按钮1 测试 /// </summary> /// <returns></returns> private ReturnValue VerifyEmpty() { ReturnValue rv = new ReturnValue(); if (string.IsNullOrEmpty(this.textBox1.Text.Trim())) { rv.Fail("值为空"); } else { rv.Success("值非空"); } return rv; } /// <summary> /// 返回值输出 /// </summary> /// <param name="rv"></param> private void ReturnValueHelper(ReturnValue rv) { bool state = rv.State; StringBuilder sb = new StringBuilder(); if (state) { sb.AppendLine("返回值状态是:True"); sb.AppendLine("成功消息:" + rv.SuccessMessage); } else { sb.AppendLine("返回值状态是:False"); sb.AppendLine("失败消息:" + rv.FailMessage); } MessageBox.Show(sb.ToString()); } #endregion
泛型:
#region 返回值(泛型) private void btnQueryStudent_Click(object sender, EventArgs e) { //获取姓名 string studentName = this.textBox2.Text.Trim(); //获取返回值 ReturnValues<int> rv = this.GetReturnValues(studentName); //输出 this.ReturnValuesHelper(rv); } /// <summary> /// 获取返回值 /// </summary> /// <param name="rv"></param> private ReturnValues<int> GetReturnValues(string name) { //实例化返回类 ReturnValues<int> rv = new ReturnValues<int>(); //构造测试数据,模拟实际开发的取数据操作 Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("A", 1); dic.Add("B", 2); dic.Add("C", 3); dic.Add("D", 4); dic.Add("E", 5); //开始判断 if (dic.Keys.Contains(name)) { rv.Success("数据查找成功"); rv.Value = dic[name]; } else { rv.Fail("数据查找失败"); } return rv; } /// <summary> /// 返回值输出 /// </summary> /// <param name="rv"></param> private void ReturnValuesHelper(ReturnValues<int> rv) { bool state = rv.State; StringBuilder sb = new StringBuilder(); if (state) { sb.AppendLine("返回值状态是:True"); sb.AppendLine("成功消息:" + rv.SuccessMessage); sb.AppendLine("泛型值:" + rv.Value.ToString()); } else { sb.AppendLine("返回值状态是:False"); sb.AppendLine("失败消息:" + rv.FailMessage); } MessageBox.Show(sb.ToString()); } #endregion }
3.开始演示:
//第二个文本只能输入A B C D E才提示成功并且可以得到对应的返回值 Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("A", 1); dic.Add("B", 2); dic.Add("C", 3); dic.Add("D", 4); dic.Add("E", 5);