3 using System
.Runtime
.CompilerServices
;
4 using System
.Reflection
;
8 const int StepSize
= 5;
9 const int Iterations
= 8;
11 public static void Main ()
13 AppDomain
.CurrentDomain
.UnhandledException
+= new UnhandledExceptionEventHandler(HandleException
);
14 var args
= new object[] {C.Iterations * C.StepSize}
;
15 typeof (C
).GetMethod ("InvokeChain", BindingFlags
.NonPublic
| BindingFlags
.Instance
).Invoke (new C (), args
);
18 public static void HandleException (object sender
, UnhandledExceptionEventArgs e
)
20 var ex
= e
.ExceptionObject
as Exception
;
24 string fullTrace
= ex
.StackTrace
;
26 string[] frames
= fullTrace
.Split(new string[] { Environment.NewLine }
, StringSplitOptions
.None
);
27 // Console.WriteLine("Split into {0} lines", frames.Length);
30 for (int i
=0; i
< frames
.Length
; i
++)
32 var words
= frames
[i
].Split((char []) null, StringSplitOptions
.RemoveEmptyEntries
);
33 if (words
.Length
> 1 && words
[0] == "at" && words
[1].StartsWith("C.InvokeChain"))
35 // Console.WriteLine("Line: {0} {1}", frames [i], words.Length);
40 if (count
> 1 && count
!= C
.StepSize
)
42 Console
.WriteLine("Fail step size");
45 if (count
== C
.StepSize
)
49 ex
= ex
.InnerException
;
52 if (iterations
!= C
.Iterations
) {
53 Console
.WriteLine ("{0} iterations", iterations
);
57 // Prevents the runtime from printing the exception
62 [MethodImpl(MethodImplOptions
.NoInlining
)]
63 private void InvokeChain (int depth
)
67 } else if (depth
% C
.StepSize
== 0) {
68 //Console.WriteLine ("InvokeChain {0} indirect", depth);
69 typeof (C
).GetMethod ("InvokeChain", BindingFlags
.NonPublic
| BindingFlags
.Instance
).Invoke (this, new object[] {depth - 1}
);
71 //Console.WriteLine ("InvokeChain {0} direct", depth);
72 InvokeChain (depth
- 1);
76 [MethodImpl(MethodImplOptions
.NoInlining
)]
79 throw new NotImplementedException ();