0

I'm having trouble understanding how I should go about adding a dynamic object to a session variable. EDIT: I don't think I explained myself well enough. Let me try again.

Example:
Object1
Property1 = Value1
Property2 = Value2
Property3 = Value3
(All of which are generated dynamically from the database.)

object obj = new object();
obj.add(property = value) // Not sure of syntax, but I'd be looping this to add stuff.
// Then when it's done looping...
HttpContext.Current.Session[Object1] = obj;
3
  • What's the problem? You define a class, instantiate it, set its properties and put it in the session. If you have a list of values from the database then you use collections and so on... Commented Apr 3, 2012 at 17:18
  • Can you tell what you want to accomplish with given existing code snippet and explain why you need to store the session object ? Commented Apr 3, 2012 at 17:19
  • @DarinDimitrov Made a revision, perhaps it's more clear now? Commented Apr 3, 2012 at 17:36

4 Answers 4

2

It sounds like you might be looking for the ExpandoObject class. This class implements IDictionary<string, object>, allowing it to store name/value pairs. It also implements IDynamicMetaObjectProvider; this implementation allows you, by way of the dynamic type (in C# 4.0 and higher), to use normal "dot" syntax to read and write properties whose names are known at compile time (that is, variable.Member).

This example assumes that you have a function GetPropertyValues() that returns an IEnumerable<KeyValuePair<string, object>> with the properties' names and values:

var obj = new ExpandoObject();
var propertyValues = GetPropertyValues();

foreach (var nameValuePair in propertyValues)
    obj[nameValuePair.Key] = nameValuePair.Value;

HttpContext.Current.Session[Object1] = obj;

If you know that the object has an ID property and a Status property, for example, you could use it like this:

dynamic obj = HttpContext.Current.Session[Object1];
var identifier = obj.ID; // basically the same as var ID = obj["ID"];
obj.Status = GetStatusForID(identifier);

//you can also add properties just by writing to them:
obj.ThisPropertyMightNotExist = "moo";
HttpContext.Current.Session[Object1] = obj;
Sign up to request clarification or add additional context in comments.

Comments

0

From ASP.net Forum

The syntax to declare a session variable is

session[VariableName] = Value;

To retrieve the value for session

string strValueFromSession = session[VariableName].ToString();

Keep in mind everytime you want to refresh it, you'll have to store it again.

Comments

0

I don't think the objects themselves are dynamically generated from the database. It's more likely that the objects are populated from the database, in which case you would store in in session just like anything else:

Session["MyObject"] = someObject; //your object

To retrieve the item from session, you would use a cast:

var obj = Session["MyObject"] as MyObject;
if (obj != null)
{
    Response.Write(obj.SomeProperty);
}

Comments

0
class DynamicClass
{
    public String DynamicObject;
}

public dynamic DynamicProperty
{
    get
    {
        return Session["Dynamic"];
    }
    set
    {
        Session["Dynamic"] = value;
    }
}

DynamicClass d = new DynamicClass();
d.DynamicObject = "Hi";
dynamic Obj = d;
DynamicProperty = Obj;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.