locked
How to get the type stored in session RRS feed

  • Question

  • User-1639794203 posted

    For instance:

    Let say I have an employee class and stored in a session. Now how can I get the Type stored in a session (In this case is Employee)?

    Employee emp = new Employee();

    Session["SomeKey'] = emp;

    string type = What code should i write to get desired result from session?

    Expecting Result:  type  = Employee

    Friday, December 16, 2016 12:56 PM

Answers

  • User-2057865890 posted

    Hi Hafeef,

    You could use Type.GetGenericArguments Method ()

    public class FooClass
    {
         public int MyProperty { get; set; }
    }
    
    
    Session["v2"] = new List<FooClass>
    {
        new FooClass { MyProperty = 1 }
    };
    
    var t = Session["v2"];
    
    Response.Write(t.GetType().GetGenericArguments()[0]);

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 27, 2016 7:42 AM

All replies

  • User-691209617 posted

    try this 

    Session("v1").GetType()
    Friday, December 16, 2016 1:09 PM
  • User-2057865890 posted

    Hi Hafeef,

    In C#, you should always cast to the appropriate type when reading session values.

    if(Session["SomeKey"]!=null)
    { Employee type = (Employee)(Session["SomeKey"]);
    }

    reference: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.session.aspx

    Best Regards,

    Chris

    Friday, December 16, 2016 3:43 PM
  • User753101303 posted

    Hi,

    You could also use something such as :

    https://www.codeproject.com/Articles/16656/Manage-ASP-NET-Session-Variables-using-the-Facade

    The idea is to access to session variables by going through a strongly typed class which gives you instant access to known values stored in your session and without having to cast them everywhere etc...

    Friday, December 16, 2016 4:37 PM
  • User-1639794203 posted

    try this 

    Session("v1").GetType()

    Thanks for the quick response. Unfortunately, didn't solve my issues. let say I have stored List<Some.Foo.FooClass> in session variable. Please find the output below after using your code.

    Output: System.Collections.Generic.List`1[[Some.Foo.FooClass, Some.Foo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

    Expected output: Some.Foo.FooClass

    Saturday, December 17, 2016 1:41 PM
  • User-2057865890 posted

    Hi Hafeef,

    You could use Type.GetGenericArguments Method ()

    public class FooClass
    {
         public int MyProperty { get; set; }
    }
    
    
    Session["v2"] = new List<FooClass>
    {
        new FooClass { MyProperty = 1 }
    };
    
    var t = Session["v2"];
    
    Response.Write(t.GetType().GetGenericArguments()[0]);

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 27, 2016 7:42 AM
  • User-1639794203 posted

    Thank you so much for your response. It helped me a lot.

    Monday, January 2, 2017 5:14 AM
  • User1539018321 posted

    Session["SomeKey"].GetType();

    Thursday, January 12, 2017 6:49 AM