Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:06:53+00:00 2026-05-17T17:06:53+00:00

I have an object, MySession , that has a hashtable for storing arbitrary properties

  • 0

I have an object, MySession, that has a hashtable for storing arbitrary properties with arbitrary types. The relevant part of the object definition is:

public class MySession
{
    private Hashtable _sessionVars;

    /// 
    /// Set and retrieve session variables ala the traditional session managers.
    /// So, SessionObject["var1"] can be used to set or retrieve a value for var1.
    /// 
    /// Name of the variable to access.
    /// An object that was stored in the session under key.
    public object this[string key] {
        get {
            if (_sessionVars.ContainsKey(key)) {
                return this._sessionVars[key];
            }

            return null;
        }
        set {
            if (this._sessionVars.ContainsKey(key)) {
                this._sessionVars.Remove(key);
            }
            this._sessionVars[key] = value;
        }
    }
}

The annoying thing is that I have to properly cast the properties when I want to use them. For example:

MySession session = new MySession();
if ( (bool)session["valid"] == true ) { /* do something fun */ }

I would rather be able to do:

MySession session = new MySession();
if ( session["valid"] == true ) { /* do something fun */ }

Is it possible to do this in C#? If so, how?

Update: I do not want to use explicit methods for accessing the properties. The point is to be able to access them as simply as possible. Not like session.GetProperty(name, type) or something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:06:54+00:00Added an answer on May 17, 2026 at 5:06 pm

    If you are using .Net Framework 4.0 then you can do it by deriving your MySession class from DynamicObject and overriding the necessary methods.

    Here is the code:

    public class MySession : DynamicObject
    {
        //Why not use Dictionary class?
        private Hashtable _sessionVars = new Hashtable();
    
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this[binder.Name] = value;
            return true;
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = this[binder.Name];
            return true;
        }
    
        //You can make it private so that users do not use strings directly.
        public object this[string key]
        {
            get
            {
                if (_sessionVars.ContainsKey(key))
                {
                    return this._sessionVars[key];
                }
    
                return null;
            }
            set
            {
                if (this._sessionVars.ContainsKey(key))
                {
                    this._sessionVars.Remove(key);
                }
                this._sessionVars[key] = value;
            }
        }
    }
    

    And this how you use it:

            dynamic ses = new MySession();
            ses.number = 5;
            ses.boolean = true;
    
            Console.WriteLine(ses.number > 4);
            if (ses.boolean)
            {
                Console.WriteLine(ses.number - 1);
            }
            Console.ReadKey();
    

    No need for casting or using string to access the new fields! If you are using Resharper you will get intellisense for existing fields too. If you need more functionality you can override other members too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have object A which in turn has a property of type Object B
I have an object that is mapped to a cookie as a serialized base-64
I have an object in a multi-threaded environment that maintains a collection of information,
I have an object of class F. I want to output the contents of
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object, that is facing a particular direction with (for instance) a
I have a Moose class that i would like to store using Apache::Session::File. However,
I have an object graph serialized to xaml. A rough sample of what it
I have an object of the type System.Drawing.Image and want to make every pixel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.