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)); }
public void get_int_from_statebag() { var collection = new StateBag { { "appId", "123" } }; var value = collection.Get <int>("appId"); Expect(value, Is.EqualTo(123)); }
/// <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); }
/// <summary> /// Returns a typed value from the ASP.NET ViewState /// </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 Get <T>(this StateBag state, string key) { return(state.Get(key, default(T))); }
public void Get_ShouldReturnDefaultValueForNonExistingItemIfNoFallback() { Assert.That(_stateBag.Get <string>("foo"), Is.Null); }