C#之專案常用方法之靜態擴充套件
阿新 • • 發佈:2019-08-26
一般專案中我們經常用到資料Json的序列化與反序列化,為了方便在需要的地方快速使用,一般建議都封裝為靜態擴充套件方法,在需要的地方可直接使用。
而目前C#專案中序列化一般都是用的 Newtonsoft.Json 來做的,因此這裡貼出通用靜態擴充套件方法供大家參考。
PS:若要新增此擴充套件方法,必須得在Nuget包中新增 Newtonsoft.Json 的引用。
using System; using System.Collections.Generic; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; namespace DemoFrame_Basic.Extensions { /// <summary> /// 靜態幫助類 /// </summary> public static class ObjStrExtension { /// <summary> /// 返回Json字串 /// </summary> /// <param name="obj">需要序列化的物件 例:T.ToJson()</param> /// <param name="isNullValueHandling">是否忽略Null欄位,最終字串中是否包含Null欄位</param> /// <param name="indented">是否展示為具有Json格式的字串</param> /// <param name="isLowCase">是否忽略大小寫</param> /// <param name="dateTimeFormat">時間轉換格式</param> /// <returns>Json字串</returns> public static string ToJson(this object obj, bool isNullValueHandling = false, bool indented = false, bool isLowCase = false, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss") { var options = new JsonSerializerSettings(); if (indented) options.Formatting = Formatting.Indented; if (isLowCase) { options.ContractResolver = new CamelCasePropertyNamesContractResolver(); } if (isNullValueHandling) options.NullValueHandling = NullValueHandling.Ignore; options.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.Converters = new List<JsonConverter> { new IsoDateTimeConverter { DateTimeFormat = dateTimeFormat } }; return obj.ToJson(options); } /// <summary> /// Json字串 /// </summary> /// <param name="obj">需要序列化的物件 例:T.ToJson(settings)</param> /// <param name="settings">Json序列化設定</param> /// <returns>Json字串</returns> public static string ToJson(this object obj, JsonSerializerSettings settings) { return JsonConvert.SerializeObject(obj, settings); } /// <summary> /// 返回相關設定格式字串 /// </summary> /// <param name="obj"></param> /// <param name="format">格式 例:yyyy-MM-dd HH:mm:ss</param> /// <returns>設定格式字串</returns> public static string ToDateTimeString(this object obj, string format) { DateTime.TryParse(obj?.ToString(), out var dateTime); return dateTime.ToString(format); } /// <summary> /// 得到字串的Byte /// </summary> /// <param name="str"></param> /// <returns>Byte</returns> public static byte[] GetBytes(this string str) { if (string.IsNullOrEmpty(str)) return default(byte[]); return Encoding.UTF8.GetBytes(str); } public static bool ToBool(this string str, bool defaultValue = false) { bool.TryParse(str, out defaultValue); return defaultValue; } public static short ToShort(this string str, short defaultValue = 0) { short.TryParse(str, out defaultValue); return defaultValue; } public static int ToInt(this string str, int defaultValue = 0) { int.TryParse(str, out defaultValue); return defaultValue; } public static long ToLong(this string str, long defaultValue = 0) { long.TryParse(str, out defaultValue); return defaultValue; } public static double ToDouble(this string str, double defaultValue = 0) { double.TryParse(str, out defaultValue); return defaultValue; } public static TEnum ToEnum<TEnum>(this string str, bool ignoreCase = true, TEnum defaultValue = default(TEnum)) where TEnum : struct { Enum.TryParse(str, ignoreCase, out defaultValue); return defaultValue; } public static T ToNetType<T>(this string str, bool isIgnoreNull = true, bool isIgnoreEx = false) { var setting = new JsonSerializerSettings { NullValueHandling = isIgnoreNull ? NullValueHandling.Ignore : NullValueHandling.Include }; try { if (string.IsNullOrEmpty(str)) { return default(T); } else if ("\"\"" == str) { return default(T); } else { return JsonConvert.DeserializeObject<T>(str, setting); } } catch (Exception) { if (!isIgnoreEx) throw; return default(T); } } public static T ToNetType<T>(this string str, JsonSerializerSettings settings) { try { if (string.IsNullOrEmpty(str)) { return default(T); } else if ("\"\"" == str) { return default(T); } else { return JsonConvert.DeserializeObject<T>(str, settings); } } catch (Exception) { return default(T); } } /// <summary> /// 比較是否相等,忽略大小寫 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static bool EqualsIgnoreCase(this string a, string b) { if (a != null) return a.Equals(b, StringComparison.CurrentCultureIgnoreCase); return b == null; } } }
&n