// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Diagnostics; using System.Reflection; namespace E { public class TestRefOne { public void Sub3() { MethodBase method = (new StackTrace(true)).GetFrame(0).GetMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefOne.Sub3") { throw new Exception("Ooops"); } } public void Sub2() { MethodBase method = (new StackTrace(true)).GetFrame(0).GetMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefOne.Sub2") { throw new Exception("Ooops"); } Sub3(); } public void Sub1() { MethodBase method = (new StackTrace(true)).GetFrame(0).GetMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefOne.Sub1") { throw new Exception("Ooops"); } Sub2(); } } public class TestRefTwo { public void Sub3() { MethodBase method = MethodInfo.GetCurrentMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefTwo.Sub3") { throw new Exception("Ooops"); } } public void Sub2() { MethodBase method = MethodInfo.GetCurrentMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefTwo.Sub2") { throw new Exception("Ooops"); } Sub3(); } public void Sub1() { MethodBase method = MethodInfo.GetCurrentMethod(); string methodname = method.DeclaringType.FullName + "." + method.Name; if(methodname != "E.TestRefTwo.Sub1") { throw new Exception("Ooops"); } Sub2(); } } public class TestAop { public void Sub3() { if(MethodTracker.GetCurrentMethod() != "E.TestAop.Sub3") { throw new Exception("Ooops"); } } public void Sub2() { if(MethodTracker.GetCurrentMethod() != "E.TestAop.Sub2") { throw new Exception("Ooops"); } Sub3(); } public void Sub1() { if(MethodTracker.GetCurrentMethod() != "E.TestAop.Sub1") { throw new Exception("Ooops"); } Sub2(); } } public class MainClass { private const int N = 100000; public static void Main(string[] args) { TestRefOne tc1 = new TestRefOne(); DateTime t1 = DateTime.Now; for(int i = 0; i < N; i++) { tc1.Sub1(); } DateTime t2 = DateTime.Now; Console.WriteLine("StackTrace: " + (t2 - t1).TotalSeconds); TestRefTwo tc2 = new TestRefTwo(); DateTime t3 = DateTime.Now; for(int i = 0; i < N; i++) { tc2.Sub1(); } DateTime t4 = DateTime.Now; Console.WriteLine("GetCurrentMethod: " + (t4 - t3).TotalSeconds); TestAop tc3 = new TestAop(); DateTime t5 = DateTime.Now; for(int i = 0; i < N; i++) { tc3.Sub1(); } DateTime t6 = DateTime.Now; Console.WriteLine("AOP: " + (t6 - t5).TotalSeconds); } } }