标签:
using System; using System.Diagnostics; using System.Reflection; using System.Web.SessionState; namespace Ztest { public class Program: IRequiresSessionState { public static void Main(string[] args) { try { if (Test.Current.session == null) { Console.WriteLine("没有继承IRequiresSessionState"); } else { Console.WriteLine(Test.Current.session); } } catch (Exception ex) { } Console.ReadLine(); } } public class Test { private Test() { Type basetype = typeof(IRequiresSessionState); StackTrace trace = new StackTrace(); int i = 0; Type type; while (true) { ///找到外层第一个调用类 MethodBase methodName = trace.GetFrame(i).GetMethod(); type = methodName.ReflectedType; if (type != typeof(Test)) { break; } i++; } Boolean key = basetype.IsAssignableFrom(type); if (key) { session = _m; } else { session = null; } } private static Test _Current; private string _m = "当前类实现了IRequiresSessionState"; /// <summary> /// 模拟session /// </summary> public Object session { get; set; } public static Test Current { get { return get(); } set { Current = value; } } private static Test get() { if (_Current == null) { _Current = new Test(); } return _Current; } } }
标签:
原文地址:http://www.cnblogs.com/linxingxunyan/p/5782172.html