69 lines
9.5 KiB
Plaintext
69 lines
9.5 KiB
Plaintext
[
|
||
{
|
||
"Id": 58085121794053,
|
||
"Title": "关于implicit 隐式转换的使用",
|
||
"Summary": "implicit 关键字用于声明隐式的用户自定义的类型转换运算符。 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换",
|
||
"Cover": "https://oss.okay123.top/oss//2023/07/13/3zM2Yx1A7o.jpg",
|
||
"IsTop": "1",
|
||
"Views": 241,
|
||
"Author": "可乐不加冰",
|
||
"Link": "",
|
||
"CreationType": 0,
|
||
"Content": " 使用隐式转换操作符之后,在编译时会跳过异常检查,所以隐式转换运算符应当从不引发异常并且从不丢失信息,否则在运行时会出现一些意想不到的问题。\n在使用API返回结果时我们约定返回统一结果。\n## 封装示例\n```C#\n /// <summary>\n /// API统一返回结果\n /// </summary>\n public class AjaxResult\n {\n /// <summary>\n /// 是否成功\n /// </summary>\n public bool Success { get; set; }\n\n /// <summary>\n /// 消息\n /// </summary>\n public string Message { get; set; }\n\n /// <summary>\n /// 状态码\n /// </summary>\n public ReturnCode Code { get; set; }\n\n /// <summary>\n /// 返回错误消息\n /// </summary>\n /// <param name=\"msg\"></param>\n public static implicit operator AjaxResult(string msg)\n {\n return new AjaxResult { Message = msg };\n }\n\n /// <summary>\n /// 直接返回是否成功\n /// </summary>\n /// <param name=\"success\"></param>\n public static implicit operator AjaxResult(bool success)\n {\n return new AjaxResult() { Success = success, Message = success ? \"操作成功\" : \"操作失败\" };\n }\n\n /// <summary>\n /// 返回错误消息\n /// </summary>\n /// <param name=\"exception\">异常</param>\n public static implicit operator AjaxResult(Exception exception)\n {\n return new AjaxResult() { Message = exception?.Message ?? \"操作失败\" };\n }\n\n /// <summary>\n /// 根据枚举返回信息\n /// </summary>\n /// <param name=\"code\">状态码</param>\n public static implicit operator AjaxResult(ReturnCode code)\n {\n return new AjaxResult() { Code = code, Message = code.ToString(), Success = code == ReturnCode.OK };\n }\n }\n\n /// <summary>\n /// API统一返回结果\n /// </summary>\n /// <typeparam name=\"T\"></typeparam>\n public class AjaxResult<T> : AjaxResult\n {\n /// <summary>\n /// 数据\n /// </summary>\n public T Data { get; set; }\n\n /// <summary>\n /// 直接返回数据对象\n /// </summary>\n /// <param name=\"data\"></param>\n public static implicit operator AjaxResult<T>(T data)\n {\n return new AjaxResult<T> { Data = data, Success = true, Code=ReturnCode.OK,Message = ReturnCode.OK.ToString() };\n }\n\n /// <summary>\n /// 返回错误消息\n /// </summary>\n /// <param name=\"exception\"></param>\n public static implicit operator AjaxResult<T>(Exception exception)\n {\n return new AjaxResult<T>() { Message = exception?.Message ?? \"操作失败\" };\n }\n\n /// <summary>\n /// 获取结果\n /// </summary>\n /// <param name=\"val\"></param>\n public static implicit operator T(AjaxResult<T> data)\n {\n return data.Data;\n }\n\n ///// <summary>\n ///// 错误消息(与AjaxResult<string>有冲突,如果返回string类型数据请使用AjaxResult<dynamic>)\n ///// </summary>\n /// <param name=\"msg\">错误消息</param>\n public static implicit operator AjaxResult<T>(string msg)\n {\n return new AjaxResult<T>() { Message = msg };\n }\n\n /// <summary>\n /// 根据枚举返回信息\n /// </summary>\n /// <param name=\"code\">状态码</param>\n public static implicit operator AjaxResult<T>(ReturnCode code)\n {\n return new AjaxResult<T>() { Code = code, Message = code.ToString(), Success = code == ReturnCode.OK };\n }\n }\n\n /// <summary>\n /// 状态码\n /// </summary>\n public enum ReturnCode\n {\n [Description(\"操作成功\")]\n OK,\n\n [Description(\"数据已禁用\")]\n Disable,\n\n [Description(\"信息不存在\")]\n NotExist,\n\n [Description(\"用户未登录\")]\n UnAuthorized,\n\n [Description(\"数据验证失败\")]\n VerificationFailed,\n\n [Description(\"操作失败\")]\n Failed = 500\n } \n```\n## 调用示例\n``` C#\nclass Program\n{\n static void Main(string[] args)\n {\n var result1 = GetError();\n string result2 = GetInfo();\n }\n\n\n public static AjaxResult<string> GetError()\n {\n var ex = new Exception(\"测试一下\");\n return ex;\n }\n\n public static AjaxResult<string> GetInfo()\n {\n return \"\";\n }\n}\n```",
|
||
"IsHtml": "0",
|
||
"PublishTime": "2023-06-01",
|
||
"Status": 0,
|
||
"Sort": 100,
|
||
"IsAllowComments": "1",
|
||
"ExpiredTime": "2025-06-01",
|
||
"DeleteMark": "0",
|
||
"UpdatedTime": "2023-06-01",
|
||
"CreatedUserId": 1,
|
||
"CreatedTime": "2023-06-01"
|
||
},
|
||
{
|
||
"Id": 61986292031493,
|
||
"Title": "对手机号以及邮箱进行隐私处理",
|
||
"Summary": "对手机号以及邮箱进行隐私处理,使用*替换部分字符",
|
||
"Cover": "https://oss.okay123.top/oss//2023/07/13/bRnXB7wM1M.png",
|
||
"IsTop": "0",
|
||
"Views": 116,
|
||
"Author": "可乐",
|
||
"Link": "",
|
||
"CreationType": 0,
|
||
"Content": "## 代码示例\n``` C#\n \t/// <summary>\n /// 字符串掩码\n /// </summary>\n /// <param name=\"s\">字符串</param>\n /// <param name=\"mask\">掩码符</param>\n /// <returns></returns>\n public static string Mask(this string s, char mask = '*')\n {\n if (string.IsNullOrWhiteSpace(s?.Trim()))\n {\n return s;\n }\n s = s.Trim();\n string masks = mask.ToString().PadLeft(4, mask);\n return s.Length switch\n {\n >= 11 => Regex.Replace(s, \"(.{3}).*(.{4})\", $\"$1{masks}$2\"),\n 10 => Regex.Replace(s, \"(.{3}).*(.{3})\", $\"$1{masks}$2\"),\n 9 => Regex.Replace(s, \"(.{2}).*(.{3})\", $\"$1{masks}$2\"),\n 8 => Regex.Replace(s, \"(.{2}).*(.{2})\", $\"$1{masks}$2\"),\n 7 => Regex.Replace(s, \"(.{1}).*(.{2})\", $\"$1{masks}$2\"),\n 6 => Regex.Replace(s, \"(.{1}).*(.{1})\", $\"$1{masks}$2\"),\n _ => Regex.Replace(s, \"(.{1}).*\", $\"$1{masks}\")\n };\n }\n```",
|
||
"IsHtml": "0",
|
||
"PublishTime": "2023-06-01",
|
||
"Status": 0,
|
||
"Sort": 100,
|
||
"IsAllowComments": "1",
|
||
"ExpiredTime": "2025-06-01",
|
||
"DeleteMark": "0",
|
||
"UpdatedTime": "2023-06-01",
|
||
"CreatedUserId": 1,
|
||
"CreatedTime": "2023-06-01"
|
||
},
|
||
{
|
||
"Id": 62763584290821,
|
||
"Title": "通过委托动态为对象属性赋值",
|
||
"Summary": "通过委托动态为对象属性赋值,避免使用反射导致性能影响",
|
||
"Cover": "https://oss.okay123.top/oss//2023/07/13/E6pMz7NN71.jpg",
|
||
"IsTop": "0",
|
||
"Views": 142,
|
||
"Author": "可乐不加冰",
|
||
"Link": "",
|
||
"CreationType": "0",
|
||
"Content": "# 封装示例\n```csharp\npublic static class ObjectExtensions\n{\n /// <summary>\n /// 动态获取对象属性的值\n /// </summary>\n /// <typeparam name=\"T\">目标对象类型</typeparam>\n /// <param name=\"o\">目标对象</param>\n /// <param name=\"propertyName\">属性名</param>\n /// <returns></returns>\n public static object GetPropertyValue<T>(this T o, string propertyName)\n {\n if (o == null)\n {\n return null;\n }\n\n Type type = o.GetType();\n PropertyInfo property = type.GetProperty(propertyName);\n if (property == null || property.GetGetMethod() == null)\n {\n return null;\n }\n var getValue = (Func<T, object>)Delegate.CreateDelegate(typeof(Func<T, object>), property.GetGetMethod());\n return getValue(o);\n }\n\n /// <summary>\n /// 动态设置对象属性的值\n /// </summary>\n /// <typeparam name=\"T\">属性值的类型</typeparam>\n /// <param name=\"o\">目标对象</param>\n /// <param name=\"propertyName\">属性名</param>\n /// <param name=\"value\">属性值</param>\n public static void SetPropertyValue<T>(this object o, string propertyName, T value)\n {\n if (o == null)\n {\n return;\n }\n\n Type type = o.GetType();\n PropertyInfo property = type.GetProperty(propertyName);\n if (property == null || property.GetSetMethod() == null)\n {\n return;\n }\n var setValue = (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), o, property.GetSetMethod());\n setValue(value);\n }\n}\n```",
|
||
"IsHtml": "0",
|
||
"PublishTime": "2023-06-01",
|
||
"Status": 0,
|
||
"Sort": 100,
|
||
"IsAllowComments": "1",
|
||
"ExpiredTime": "2025-06-01",
|
||
"DeleteMark": "0",
|
||
"UpdatedTime": "2023-06-01",
|
||
"CreatedUserId": 1,
|
||
"CreatedTime": "2023-06-01"
|
||
}
|
||
]
|