执行某个方法后, 一般都要对执行结果判断, 如果执行不成功, 还需要显示错误信息, 我先后使用了下面几种方式
/// <summary> /// 返回int类型结果, msg输出错误信息 /// </summary> /// <param name="param">输入参数</param> /// <param name="msg">错误信息</param> /// <returns>0-成功, 其他值-失败</returns> public int Foo(object param, out string msg);
/// <summary> /// 直接返回值(string)为空, 表示成功; 不成功的话, 输出错误信息 /// </summary> /// <param name="param">输入参数</param> /// <param name="result">结算结果</param> /// <returns>null或者""(string.Empty)-成功, 非空-失败</returns> public string Bar(object param, out object result);
public struct Result { /// <summary> /// 执行结果 /// </summary> public int Code; /// <summary> /// 错误信息 /// </summary> public string Msg; } /// <summary> /// 直接返回Result结果, Result.Code表示执行结果, Result.Msg包含错误信息 /// </summary> /// <param name="param"></param> /// <param name="calculateResult"></param> /// <returns>Result.Code表示执行结果, Result.Msg包含错误信息</returns> public Result Baz(object param, out object calculateResult); public void Test() { object calculateResult; var result = Baz(null, out calculateResult); if (result.Code!=0) { // ... } }
第3种方法, 每次判断都需要键入Code, 有点麻烦, 可以使用C#的运算符重载简化一点点代码
1 /// <summary> 2 /// 带结果代码和提示信息的结构体, 可以很方便的以结果代码(Code)与int比较, Code==0表示成功 3 /// </summary> 4 public struct Result 5 { 6 /// <summary> 7 /// 结果代码 8 /// </summary> 9 public int Code; 10 11 /// <summary> 12 /// 提示信息 13 /// </summary> 14 public string Msg; 15 16 17 18 #region 构造函数 19 20 public Result(string msgParam) 21 : this(-1, msgParam) 22 { } 23 24 25 public Result(int codeParam, string msgParam) 26 { 27 Code = codeParam; 28 Msg = msgParam; 29 } 30 31 #endregion 32 33 34 35 #region 快速生成结构体 36 public static Result Pass { get { return new Result(); } } 37 38 39 public static Result PassWithMsg(string msgParam) 40 { 41 return new Result() { Code = 0, Msg = msgParam }; 42 } 43 44 45 public static Result Fail(string msgParam) 46 { 47 return new Result(msgParam); 48 } 49 50 //如果codeParam==0 的话, 强制变 -1 51 public static Result Fail(int codeParam, string msgParam) 52 { 53 codeParam = codeParam == 0 ? -1 : codeParam; 54 return new Result(codeParam, msgParam); 55 } 56 #endregion 57 58 59 60 #region 重载与int的比较 61 62 public static bool operator ==(int lhs, Result rhs) 63 { 64 return lhs == rhs.Code; 65 } 66 67 public static bool operator !=(int lhs, Result rhs) 68 { 69 return lhs != rhs.Code; 70 } 71 72 public static bool operator ==(Result lhs, int rhs) 73 { 74 return rhs == lhs; 75 } 76 77 public static bool operator !=(Result lhs, int rhs) 78 { 79 return rhs != lhs; 80 } 81 #endregion 82 83 84 85 #region 还得实现同类型的比较 86 87 public static bool operator ==(Result lhs, Result rhs) 88 { 89 return lhs.Code == rhs.Code; 90 } 91 92 public static bool operator !=(Result lhs, Result rhs) 93 { 94 return lhs.Code != rhs.Code; 95 } 96 #endregion 97 98 99 100 #region 按要求重载 Equals()和GetHashCode()两个方法, 完全以Code为比较值 101 102 public override bool Equals(object obj) 103 { 104 if (obj is int) 105 { 106 return this.Code == (int)obj; 107 } 108 if (obj is Result == false) 109 { 110 return false; 111 } 112 return this.Code == ((Result)obj).Code; 113 } 114 115 public override int GetHashCode() 116 { 117 return this.Code.GetHashCode(); 118 } 119 120 #endregion 121 122 123 124 125 #region int显示转换为Result结构(示例: Result result=(Result)0; ), 弃用这个的原因是: 当结果不为0时, 不能赋值Msg; 126 //public static explicit operator Result(int result) 127 //{ 128 // return new Result() { Code = result }; 129 //} 130 #endregion 131 }
这样的话, 就不用每次都输入Code了
class Program { static void Main(string[] args) { var result = PassMethod(); if (result==0) //直接与int比较 { Console.WriteLine("PassMethod成功"); } else { Console.WriteLine("PassMethod失败: "+result.Msg); } result = FailMethod(); if (result == Result.Pass) //与Result类型比较 { Console.WriteLine("FailMethod成功"); } else { Console.WriteLine("FailMethod失败: " + result.Msg); } } public static Result PassMethod() { return Result.Pass; } public static Result FailMethod() { return Result.Fail("Something is wrong"); } }