例如:任意类型的JSON序列化、序列化
public static class ExtMethod
{
/// <summary>
/// JSON序列化
/// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON字符串</returns>
public static string ToJson(this object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
}
/// <summary>
/// JSON反序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="str">JSON字符串</param>
/// <returns>对象</returns>
public static T FromJson<T>(this string str) where T : class
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
}
}
调用时:
LogManage.Logger.SaveLog("数据审计:" + getDiff(it).ToJson());
var s = UtilMethods.GetNativeSql(sql, p);
LogManage.Logger.SaveLog("执行超过1秒SQL:" + s);
public T Get<T>(string key) where T : class
{
var rt = this.redisClient.Get(key);
if (rt != null)
{
return rt.FromJson<T>();
}
else
{
return null;
}
}