[netcore] Ongoing work. (#13391)
[mono-project.git] / mono / tests / dynamic-method-stack-traces.cs
blob965c525ae864801741fe1c5a09b4422f1a18899a
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;
8 public class A
10 public static Exception Caught;
12 public static void ThrowMe()
14 Exception e;
15 try
17 throw new Exception("test");
19 catch (Exception e2)
21 e = e2;
24 var edi = ExceptionDispatchInfo.Capture(e);
26 edi.Throw();
29 public static void Handler(Exception e)
31 Caught = e;
35 public class Example
37 public static int Main()
39 TT();
40 string expected = A.Caught.StackTrace.ToString ();
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;
55 return 0;
58 static void TT()
60 DynamicMethod multiplyHidden = new DynamicMethod(
61 "",
62 typeof(void), new[] { typeof(int) }, typeof(Example));
64 ILGenerator ig = multiplyHidden.GetILGenerator();
66 ig.BeginExceptionBlock();
68 ig.Emit(OpCodes.Call, typeof(A).GetMethod("ThrowMe"));
70 ig.BeginCatchBlock(typeof(Exception));
72 ig.Emit(OpCodes.Call, typeof(A).GetMethod("Handler"));
74 ig.EndExceptionBlock();
76 ig.Emit(OpCodes.Ret);
78 var invoke = (Action<int>)
79 multiplyHidden.CreateDelegate(
80 typeof(Action<int>)
84 invoke(1);