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

  • SEARCH
  • Home
  • 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 885595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:56:07+00:00 2026-05-15T12:56:07+00:00

I wrote the following extension methods for Session so that I can persist and

  • 0

I wrote the following extension methods for Session so that I can persist and retrieve objects by their type. This works well for my solution, but I ended up having to duplicate my extension methods to cover the old HttpSessionState and the new HttpSessionStateBase. I’d like to find a way to get these back down to one set that covers both types. Any thoughts?

public static class SessionExtensions
{
    #region HttpSessionStateBase

    public static T Get<T>(this HttpSessionStateBase session)
    {
        return session.Get<T>(typeof(T).Name);
    }

    public static T Get<T>( this HttpSessionStateBase session, string key )
    {
        var obj = session[key];

        if( obj == null || typeof(T).IsAssignableFrom( obj.GetType() ) )
            return (T) obj;

        throw new Exception( "Type '" + typeof( T ).Name + "' doesn't match the type of the object retreived ('" + obj.GetType().Name + "')." );
    }

    public static void Put<T>(this HttpSessionStateBase session, T obj, string key)
    {
        session[key] = obj;
    }

    public static void Put<T>(this HttpSessionStateBase session, T obj)
    {
        session.Put(obj, typeof(T).Name);
    }

    #endregion

    #region HttpSessionState

    public static T Get<T>( this HttpSessionState session )
    {
        return session.Get<T>( typeof( T ).Name );
    }

    public static T Get<T>( this HttpSessionState session, string key )
    {
        var obj = session[ key ];

        if( obj == null || typeof( T ).IsAssignableFrom( obj.GetType() ) )
            return ( T ) obj;

        throw new Exception( "Type '" + typeof( T ).Name + "' doesn't match the type of the object retreived ('" + obj.GetType().Name + "')." );
    }

    public static void Put<T>( this HttpSessionState session, T obj )
    {
        session.Put( obj, typeof(T).Name );
    }

    public static void Put<T>( this HttpSessionState session, T obj, string key )
    {
        session[ key ] = obj;
    }

    #endregion
}
  • 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-15T12:56:07+00:00Added an answer on May 15, 2026 at 12:56 pm

    I found an answer that works but there are some drawbacks. I’m hoping someone will be able to improve upon it.

    @Andrew Hare said that neither implement a common base or interface. Well, actually, they do. They both implement IEnumerable and ICollection. The question is, with that information in tow, do you want to create extension methods that extend IEnumerable or ICollection that are really only meant for Session? Maybe, maybe not. At any rate, here is a way to remove duplication with extension methods that extend HttpSessionState and HttpSessionStateBase simultaneously:

    public static class SessionExtensions
    {
        public static T Get<T>( this ICollection collection )
        {
            return collection.Get<T>( typeof( T ).Name );
        }
    
        public static T Get<T>( this ICollection collection, string key )
        {
            object obj = null;
            dynamic session = collection as HttpSessionState ?? ( dynamic ) ( collection as HttpSessionStateBase );
    
            if( session != null )
            {
                obj = session[key];
    
                if (obj != null && !typeof (T).IsAssignableFrom(obj.GetType()))
                    throw new Exception("Type '" + typeof (T).Name + "' doesn't match the type of the object retreived ('" + obj.GetType().Name + "').");
            }
    
            return (T)obj;
        }
    
        public static void Put<T>( this ICollection collection, T obj )
        {
            collection.Put( obj, typeof( T ).Name );
        }
    
        public static void Put<T>( this ICollection collection, T obj, string key )
        {
            dynamic session = collection as HttpSessionState ?? ( dynamic ) ( collection as HttpSessionStateBase );
            if(session!=null)
                session[ key ] = obj;
        }
    }
    

    I’m not crazy about this solution, but it feels like a step, at least, in an interesting direction.

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

Sidebar

Related Questions

I want to be able to write extension methods so that I can say:
I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this
I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>
I want to write an extension method for a collection of objects that uses
Ok, i'm well and truly stumped here. I've written extension methods before, and never
i have wrote a extension method for customize my validation messages, like this: namespace
I wrote the following extension method to get an element from a dictionary, or
I'm a bit confused about how extension methods work. If I'm reading this correctly
I saw this question that asks given a string smith;rodgers;McCalne how can you produce
i wrote following code to create a linkbutton programmatically, but its showing like lable

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.