使用隐式转换操作符之后,在编译时会跳过异常检查,所以隐式转换运算符应当从不引发异常并且从不丢失信息,否则在运行时会出现一些意想不到的问题。
在使用API返回结果时我们约定返回统一结果。
封装示例
/// <summary>
/// API统一返回结果
/// </summary>
public class AjaxResult
{
/// <summary>
/// 是否成功
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 消息
/// </summary>
public string Message { get; set; }
/// <summary>
/// 状态码
/// </summary>
public ReturnCode Code { get; set; }
/// <summary>
/// 返回错误消息
/// </summary>
/// <param name="msg"></param>
public static implicit operator AjaxResult(string msg)
{
return new AjaxResult { Message = msg };
}
/// <summary>
/// 直接返回是否成功
/// </summary>
/// <param name="success"></param>
public static implicit operator AjaxResult(bool success)
{
return new AjaxResult() { Success = success, Message = success ? "操作成功" : "操作失败" };
}
/// <summary>
/// 返回错误消息
/// </summary>
/// <param name="exception">异常</param>
public static implicit operator AjaxResult(Exception exception)
{
return new AjaxResult() { Message = exception?.Message ?? "操作失败" };
}
/// <summary>
/// 根据枚举返回信息
/// </summary>
/// <param name="code">状态码</param>
public static implicit operator AjaxResult(ReturnCode code)
{
return new AjaxResult() { Code = code, Message = code.ToString(), Success = code == ReturnCode.OK };
}
}
/// <summary>
/// API统一返回结果
/// </summary>
/// <typeparam name="T"></typeparam>
public class AjaxResult<T> : AjaxResult
{
/// <summary>
/// 数据
/// </summary>
public T Data { get; set; }
/// <summary>
/// 直接返回数据对象
/// </summary>
/// <param name="data"></param>
public static implicit operator AjaxResult<T>(T data)
{
return new AjaxResult<T> { Data = data, Success = true, Code=ReturnCode.OK,Message = ReturnCode.OK.ToString() };
}
/// <summary>
/// 返回错误消息
/// </summary>
/// <param name="exception"></param>
public static implicit operator AjaxResult<T>(Exception exception)
{
return new AjaxResult<T>() { Message = exception?.Message ?? "操作失败" };
}
/// <summary>
/// 获取结果
/// </summary>
/// <param name="val"></param>
public static implicit operator T(AjaxResult<T> data)
{
return data.Data;
}
///// <summary>
///// 错误消息(与AjaxResult<string>有冲突,如果返回string类型数据请使用AjaxResult<dynamic>)
///// </summary>
/// <param name="msg">错误消息</param>
public static implicit operator AjaxResult<T>(string msg)
{
return new AjaxResult<T>() { Message = msg };
}
/// <summary>
/// 根据枚举返回信息
/// </summary>
/// <param name="code">状态码</param>
public static implicit operator AjaxResult<T>(ReturnCode code)
{
return new AjaxResult<T>() { Code = code, Message = code.ToString(), Success = code == ReturnCode.OK };
}
}
/// <summary>
/// 状态码
/// </summary>
public enum ReturnCode
{
[Description("操作成功")]
OK,
[Description("数据已禁用")]
Disable,
[Description("信息不存在")]
NotExist,
[Description("用户未登录")]
UnAuthorized,
[Description("数据验证失败")]
VerificationFailed,
[Description("操作失败")]
Failed = 500
}
C#
调用示例
class Program
{
static void Main(string[] args)
{
var result1 = GetError();
string result2 = GetInfo();
}
public static AjaxResult<string> GetError()
{
var ex = new Exception("测试一下");
return ex;
}
public static AjaxResult<string> GetInfo()
{
return "";
}
}
C#