public void StateBag() { object x = "a"; x+="a"; var sb = new StateBag(); sb.Set(x,"p1",20); sb.Set("aa", "p1", 50); Assert.AreEqual(20,sb.Get(x,"p1",null)); // Aa is interned Assert.IsTrue(string.IsInterned("aa")!=null); Assert.AreEqual(50, sb.Get("aa", "p1", null)); sb.Remove("aa","p1"); Assert.IsNull(sb.Get("aa", "p1", null)); }
public void StateBag() { object x = "a"; x += "a"; var sb = new StateBag(); sb.Set(x, "p1", 20); sb.Set("aa", "p1", 50); Assert.AreEqual(20, sb.Get(x, "p1", null)); // Aa is interned Assert.IsTrue(string.IsInterned("aa") != null); Assert.AreEqual(50, sb.Get("aa", "p1", null)); sb.Remove("aa", "p1"); Assert.IsNull(sb.Get("aa", "p1", null)); }
/// <summary> /// Ensures a specific key to be either already in the ASP.NET ViewState or to be newly created /// </summary> /// <typeparam name = "T">The generic type to be returned</typeparam> /// <param name = "state">The ViewState.</param> /// <param name = "key">The ViewState key.</param> /// <returns>The ViewState value.</returns> /// <example> /// <code> /// public string Text { /// get { return this.ViewState.Get<string>("Text", "DefaultText"); } /// set { this.ViewState.Set("Text", value); } /// } /// </code> /// </example> public static T Ensure <T>(this StateBag state, string key) where T : class, new() { var value = state.Get <T>(key); if (default(T) == value) { value = new T(); state.Set(key, value); } return(value); }
public void Set_ShouldAddItem() { _stateBag.Set("foo", "bar2"); Assert.That(_stateBag.Get <string>("foo"), Is.EqualTo("bar2")); }