[
{
"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 /// \n /// API统一返回结果\n /// \n public class AjaxResult\n {\n /// \n /// 是否成功\n /// \n public bool Success { get; set; }\n\n /// \n /// 消息\n /// \n public string Message { get; set; }\n\n /// \n /// 状态码\n /// \n public ReturnCode Code { get; set; }\n\n /// \n /// 返回错误消息\n /// \n /// \n public static implicit operator AjaxResult(string msg)\n {\n return new AjaxResult { Message = msg };\n }\n\n /// \n /// 直接返回是否成功\n /// \n /// \n public static implicit operator AjaxResult(bool success)\n {\n return new AjaxResult() { Success = success, Message = success ? \"操作成功\" : \"操作失败\" };\n }\n\n /// \n /// 返回错误消息\n /// \n /// 异常\n public static implicit operator AjaxResult(Exception exception)\n {\n return new AjaxResult() { Message = exception?.Message ?? \"操作失败\" };\n }\n\n /// \n /// 根据枚举返回信息\n /// \n /// 状态码\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 /// \n /// API统一返回结果\n /// \n /// \n public class AjaxResult : AjaxResult\n {\n /// \n /// 数据\n /// \n public T Data { get; set; }\n\n /// \n /// 直接返回数据对象\n /// \n /// \n public static implicit operator AjaxResult(T data)\n {\n return new AjaxResult { Data = data, Success = true, Code=ReturnCode.OK,Message = ReturnCode.OK.ToString() };\n }\n\n /// \n /// 返回错误消息\n /// \n /// \n public static implicit operator AjaxResult(Exception exception)\n {\n return new AjaxResult() { Message = exception?.Message ?? \"操作失败\" };\n }\n\n /// \n /// 获取结果\n /// \n /// \n public static implicit operator T(AjaxResult data)\n {\n return data.Data;\n }\n\n ///// \n ///// 错误消息(与AjaxResult有冲突,如果返回string类型数据请使用AjaxResult)\n ///// \n /// 错误消息\n public static implicit operator AjaxResult(string msg)\n {\n return new AjaxResult() { Message = msg };\n }\n\n /// \n /// 根据枚举返回信息\n /// \n /// 状态码\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 /// \n /// 状态码\n /// \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 GetError()\n {\n var ex = new Exception(\"测试一下\");\n return ex;\n }\n\n public static AjaxResult 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/// \n /// 字符串掩码\n /// \n /// 字符串\n /// 掩码符\n /// \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 /// \n /// 动态获取对象属性的值\n /// \n /// 目标对象类型\n /// 目标对象\n /// 属性名\n /// \n public static object GetPropertyValue(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)Delegate.CreateDelegate(typeof(Func), property.GetGetMethod());\n return getValue(o);\n }\n\n /// \n /// 动态设置对象属性的值\n /// \n /// 属性值的类型\n /// 目标对象\n /// 属性名\n /// 属性值\n public static void SetPropertyValue(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)Delegate.CreateDelegate(typeof(Action), 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"
}
]