Session에 대한 다음 확장 메서드를 작성하여 개체를 해당 형식으로 유지하고 검색 할 수 있도록했습니다. 이것은 내 솔루션에 잘 작동하지만 이전 HttpSessionState와 새로운 HttpSessionStateBase를 다루기 위해 확장 메서드를 복제해야했습니다. 이 두 가지 유형을 모두 다루는 한 세트로 다시 가져올 수있는 방법을 찾고 싶습니다. 이견있는 사람?HttpSessionState 및 HttpSessionStateBase에 대한 별도의 메서드를 작성하지 않고도 Session 확장을 작성할 수 있습니까?
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
}