c# .net list anonymous read only store dynamic types in session

If you’re new to .Net 4.0 “dynamic” type, then you may or may not be surprised to find this is reserved for unique object types with read-only properties, as compared to its previous counterpart the “object” type.

In general, this is not an issue that many will encounter, at least initially, since many implementations will probably be focused on one-way databinding to controls on pages which in turn update the database directly instead of the in-memory object storing the values from the initial data results.

However, keep in mind that as cool as dynamic is, this immutable nature makes for a mess in circumstances that require two-way binding, such as with a session variable (example below). If you are keen on using it in these scenarios, never fear, I have worked up your solution. 😉

A typical practice sometimes utilized for Session variables is to use a common class for getting/setting session values, such as (you can skip ahead if you’re familiar with this aspect):

The immutable Session scenario

public static class SessionVars {

public static object MySessionValue{get {return getval("MySessionValue");} set {setval("MySessionValue",value);}}

public static List<object> MyListOfSomething { get { return (List<object>)getval("MyListOfSomething"); } set { setval("MyListOfSomething",value); } }

        private static object getval(string key)
                return HttpContext.Current.Session[key];
            catch (Exception ex)
                //return "Error retrieving value";
return null;

        private static void setval(string key, object value)
                HttpContext.Current.Session[key] = value;
            catch (Exception ex) {
                //return "Error setting value";

The above could then be utilized such as:

SessionVars.MyListOfSomething =somevalue;
SessionVars.MyListOfSomething [0]=somevalue;
yourdatabindingcontrol.DataSource = SessionVars.MapNames;
yourdatabindingcontrol.DataBind(); //etc..

This of course makes Session usage much easier, however, our third line of code in the above example would not be possible if it were of type dynamic, which otherwise generally replaces object.

If MyListOfSomething were instead List of dynamic, you would have to use the following:


//for list
   var removei = SessionVars.MyListOfSomething [index];
                SessionVars.MyListOfSomething .Remove(removei);
                string newvalue = "somenewvalue";
                var addi = new { id = removei.id, name = removei.name, value= newvalue };
                SessionVars.MyListOfSomething .Add(addi);

//for single value
   var removei = SessionVars.MySessionValue;
                string newvalue = "somenewvalue";
                var addi = new { id = removei.id, name = removei.name, value= newvalue };

MSDN (Immutable anonymous types), c# .net list anonymous read only store dynamic types in session
Store List to Session (StackOverflow), http://stackoverflow.com/questions/1259934/store-list-to-session


About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on July 31, 2012, in Programming & Development and tagged , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: