执行某个方法后, 一般都要对执行结果判断, 如果执行不成功, 还需要显示错误信息, 我先后使用了下面几种方式
/// <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!=)
{
// ...
}
}
第3种方法, 每次判断都需要键入Code, 有点麻烦, 可以使用C#的运算符重载简化一点点代码
/// <summary>
/// 带结果代码和提示信息的结构体, 可以很方便的以结果代码(Code)与int比较, Code==0表示成功
/// </summary>
public struct Result
{
/// <summary>
/// 结果代码
/// </summary>
public int Code; /// <summary>
/// 提示信息
/// </summary>
public string Msg; #region 构造函数 public Result(string msgParam)
: this(-, msgParam)
{ } public Result(int codeParam, string msgParam)
{
Code = codeParam;
Msg = msgParam;
} #endregion #region 快速生成结构体
public static Result Pass { get { return new Result(); } } public static Result PassWithMsg(string msgParam)
{
return new Result() { Code = , Msg = msgParam };
} public static Result Fail(string msgParam)
{
return new Result(msgParam);
} //如果codeParam==0 的话, 强制变 -1
public static Result Fail(int codeParam, string msgParam)
{
codeParam = codeParam == ? - : codeParam;
return new Result(codeParam, msgParam);
}
#endregion #region 重载与int的比较 public static bool operator ==(int lhs, Result rhs)
{
return lhs == rhs.Code;
} public static bool operator !=(int lhs, Result rhs)
{
return lhs != rhs.Code;
} public static bool operator ==(Result lhs, int rhs)
{
return rhs == lhs;
} public static bool operator !=(Result lhs, int rhs)
{
return rhs != lhs;
}
#endregion #region 还得实现同类型的比较 public static bool operator ==(Result lhs, Result rhs)
{
return lhs.Code == rhs.Code;
} public static bool operator !=(Result lhs, Result rhs)
{
return lhs.Code != rhs.Code;
}
#endregion #region 按要求重载 Equals()和GetHashCode()两个方法, 完全以Code为比较值 public override bool Equals(object obj)
{
if (obj is int)
{
return this.Code == (int)obj;
}
if (obj is Result == false)
{
return false;
}
return this.Code == ((Result)obj).Code;
} public override int GetHashCode()
{
return this.Code.GetHashCode();
} #endregion #region int显示转换为Result结构(示例: Result result=(Result)0; ), 弃用这个的原因是: 当结果不为0时, 不能赋值Msg;
//public static explicit operator Result(int result)
//{
// return new Result() { Code = result };
//}
#endregion
}
这样的话, 就不用每次都输入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");
}
}