/// <summary> /// 获取session /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="key"></param> /// <returns></returns> public static T GetSession <T>(HttpSessionState session, string key) { var obj = session[key]; { //用新的方法实现 if (obj is T res) { return(res); } } if (ValidateHelper.Is <T>(obj)) { return((T)obj); } if (obj == null) { return(default(T)); } try { return(obj.ToJson().JsonToEntity <T>()); } catch { return(default(T)); } }
/// <summary> /// 获取session /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="key"></param> /// <returns></returns> public static T GetSession <T>(this HttpSessionState session, string key) { var obj = session[key]; if (obj == null) { return(default(T)); } //判断1 if (obj is T res) { return(res); } //判断2 if (ValidateHelper.Is <T>(obj)) { return((T)obj); } //使用json转换 try { return(obj.ToJson().JsonToEntity <T>()); } catch { return(default(T)); } }
public static T GetSession <T>(HttpSessionState session, string key) { if (session == null) { throw new Exception("session不存在"); } var obj = session[key]; if (ValidateHelper.Is <T>(obj)) { return((T)obj); } return(default(T)); }