[interp] disable assemblyresolve_event6.exe
[mono.git] / mono / tests / dynamic-method-stack-traces.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Reflection.Emit;
5 using System.Runtime.ExceptionServices;
6 using System.Diagnostics;
7
8 public class A
9 {
10         public static Exception Caught;
11
12         public static void ThrowMe()
13         {
14                 Exception e;
15                 try
16                 {
17                         throw new Exception("test");
18                 }
19                 catch (Exception e2)
20                 {
21                         e = e2;
22                 }
23
24                 var edi = ExceptionDispatchInfo.Capture(e);
25
26                 edi.Throw();
27         }
28
29         public static void Handler(Exception e)
30         {
31                 Caught = e;
32         }
33 }
34
35 public class Example
36 {
37         public static int Main()
38         {
39                 TT();
40                 string expected = A.Caught.StackTrace.ToString ();
41
42                 for (int i = 0; i < 1000; ++i) {
43                         Thread t = new Thread (delegate () {
44                                         TT ();
45                                 });
46                         t.Start ();
47                         t.Join ();
48                         GC.Collect ();
49                         GC.WaitForPendingFinalizers ();
50                         if (A.Caught.StackTrace != expected) {
51                                 Console.WriteLine ("FAILED");
52                                 return 1;
53                         }
54                 }
55                 return 0;
56         }
57
58         static void TT()
59         {
60                 DynamicMethod multiplyHidden = new DynamicMethod(
61                         "",
62                         typeof(void), new[] { typeof(int) }, typeof(Example));
63
64                 ILGenerator ig = multiplyHidden.GetILGenerator();
65
66                 ig.BeginExceptionBlock();
67
68                 ig.Emit(OpCodes.Call, typeof(A).GetMethod("ThrowMe"));
69
70                 ig.BeginCatchBlock(typeof(Exception));
71
72                 ig.Emit(OpCodes.Call, typeof(A).GetMethod("Handler"));
73
74                 ig.EndExceptionBlock();
75
76                 ig.Emit(OpCodes.Ret);
77
78                 var invoke = (Action<int>)
79                         multiplyHidden.CreateDelegate(
80                                 typeof(Action<int>)
81
82                         );
83
84                 invoke(1);
85         }
86 }