/// <summary> /// Event handler attached to "Page.Unload" that sticks marked fields /// into session state /// </summary> /// <param name="o">Object firing the event</param> /// <param name="e">Event arguments</param> private void SessionStore(object o, EventArgs e) { FieldInfo[] fields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Persistent state HttpCookie pageCookie = new HttpCookie(GetType().FullName); HttpCookie siteCookie = Request.Cookies[keyPrefix]; if (siteCookie == null) { siteCookie = new HttpCookie(keyPrefix); } siteCookie.Expires = pageCookie.Expires = DateTime.Now.ToUniversalTime().AddYears(2); foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(PersistentPageStateAttribute), true)) { if (field.FieldType.IsPrimitive) { PersistentPageStateAttribute ppsa = (PersistentPageStateAttribute) field.GetCustomAttributes( typeof(PersistentPageStateAttribute), true)[0]; if (ppsa.keyName == null) { pageCookie[Page.Request.Path + ":" + this.UniqueID + "." + field.Name] = (string)Convert.ChangeType(field.GetValue(this), typeof(string), CultureInfo.InvariantCulture); } else { siteCookie[ppsa.keyName] = (string)Convert.ChangeType(field.GetValue(this), typeof(string), CultureInfo.InvariantCulture); } } else { throw new SerializationException( String.Format("Field '{0}' is not a primitive type", field.Name)); } } } if (pageCookie.Values.Count > 0) { Response.AppendCookie(pageCookie); } if (siteCookie.Values.Count > 0) { Response.AppendCookie(siteCookie); } // Transient & Session scope state foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(TransientPageStateAttribute), true) || field.IsDefined(typeof(SessionPageStateAttribute), true)) { if (field.FieldType.IsSerializable) { string fieldName; fieldName = Page.Request.Path + ":" + this.UniqueID + "." + field.Name; if (field.IsDefined(typeof(SessionPageStateAttribute), true)) { SessionPageStateAttribute spsa = (SessionPageStateAttribute) field.GetCustomAttributes( typeof(SessionPageStateAttribute), true)[0]; if (spsa.keyName != null) { fieldName = keyPrefix + spsa.keyName; } } Session[fieldName] = field.GetValue(this); } else { throw new SerializationException( String.Format("Type {0} of field '{0}' is not serializable", field.FieldType.FullName, field.Name)); } } } }
/// <summary> /// Event handler attached to "Page.Init" that recovers /// marked fields from session state /// </summary> /// <param name="o">Object firing the event</param> /// <param name="e">Event arguments</param> private void SessionLoad(object o, EventArgs e) { FieldInfo[] fields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Persistent, page scope values HttpCookie pageCookie = Request.Cookies[GetType().FullName]; if (pageCookie != null) { foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(PersistentPageStateAttribute), true) && field.FieldType.IsPrimitive) { PersistentPageStateAttribute ppsa = (PersistentPageStateAttribute) field.GetCustomAttributes( typeof(PersistentPageStateAttribute), true)[0]; if (ppsa.keyName == null && pageCookie[Page.Request.Path + ":" + this.UniqueID + "." + field.Name] != null) { field.SetValue(this, Convert.ChangeType(pageCookie[Page.Request.Path + ":" + this.UniqueID + "." + field.Name], field.FieldType, CultureInfo.InvariantCulture)); } } } } // Persistent, user scope values HttpCookie siteCookie = Request.Cookies[keyPrefix]; if (siteCookie != null) { foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(PersistentPageStateAttribute), true) && field.FieldType.IsPrimitive) { PersistentPageStateAttribute ppsa = (PersistentPageStateAttribute) field.GetCustomAttributes( typeof(PersistentPageStateAttribute), true)[0]; if (ppsa.keyName != null && siteCookie[ppsa.keyName] != null) { field.SetValue(this, Convert.ChangeType(siteCookie[ppsa.keyName], field.FieldType, CultureInfo.InvariantCulture)); } } } } // Session scope values foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(SessionPageStateAttribute), true) && field.FieldType.IsSerializable) { SessionPageStateAttribute spsa = (SessionPageStateAttribute) field.GetCustomAttributes( typeof(SessionPageStateAttribute), true)[0]; if (spsa.keyName == null) { field.SetValue(this, Session[Page.Request.Path + ":" + this.UniqueID + "." + field.Name]); } else { field.SetValue(this, Session[keyPrefix + spsa.keyName]); } } } if (IsPostBack) { // Conversation scope values foreach (FieldInfo field in fields) { if (field.IsDefined(typeof(TransientPageStateAttribute), true) && field.FieldType.IsSerializable) { field.SetValue(this, Session[Page.Request.Path + ":" + this.UniqueID + "." + field.Name]); } } } }