Beispiel #1
0
        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));
        }
Beispiel #2
0
        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));
        }
Beispiel #3
0
        public void get_int_from_statebag()
        {
            var collection = new StateBag {
                { "appId", "123" }
            };

            var value = collection.Get <int>("appId");

            Expect(value, Is.EqualTo(123));
        }
Beispiel #4
0
        /// <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&lt;string&gt;("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);
        }
Beispiel #5
0
 /// <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&lt;string&gt;("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)));
 }
Beispiel #6
0
 public void Get_ShouldReturnDefaultValueForNonExistingItemIfNoFallback()
 {
     Assert.That(_stateBag.Get <string>("foo"), Is.Null);
 }