2 using System.Collections.Generic;
4 class StackTraceDumper {
6 public static void Main ()
9 throw new Exception ("Stacktrace with 1 frame");
10 } catch (Exception e) {
11 Console.WriteLine (e);
12 Console.WriteLine ("Stacktrace:");
13 Console.WriteLine (new System.Diagnostics.StackTrace(e));
16 Catch (() => {throw new Exception ("Stacktrace with 2 frames");});
18 Catch (() => ThrowException ("Stacktrace with 3 frames", 1));
20 Catch (() => ThrowException ("Stacktrace with 4 frames", 2));
23 var message = "Stack frame with method overload using ref parameter";
24 ThrowException (ref message);
29 ThrowException ("Stack frame with method overload using out parameter", out i);
32 Catch (() => ThrowExceptionGeneric<double> ("Stack frame with 1 generic parameter"));
34 Catch (() => ThrowExceptionGeneric<double,string> ("Stack frame with 2 generic parameters"));
36 Catch (() => ThrowExceptionGeneric (12));
38 Catch (() => InnerClass.ThrowException ("Stack trace with inner class"));
40 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class"));
42 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class and method generic parameter", "string"));
44 Catch (() => InnerGenericClass<string>.ThrowException<string> ("Stack trace with inner generic class and generic overload", "string"));
46 Catch (() => InnerGenericClass<string>.InnerInnerGenericClass<int>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
48 Catch (() => InnerGenericClass<int>.InnerInnerGenericClass<string>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
50 Catch (() => InnerGenericClass<int>.ThrowException ("Stack trace with nested type argument", "string", null));
53 var d = new Dictionary<string, string> ();
54 d.ContainsKey (null); // ArgumentNullException
58 The following test include ambiguous methods we can't resolve. Testing this is hard, so I'm leaving a test behind but disabling it for the time being
59 In this case the ambiguous methods are:
60 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<int> zz)
61 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<double> zz)
63 The are ambiguous because the only difference is the instantiation on the last parameter which we can't
64 figure out from a stacktrace.
66 //Catch (() => ComplicatedTestCase.Run ());
69 public static void Catch (Action action)
73 } catch (Exception e) {
75 Console.WriteLine (e);
76 Console.WriteLine ("Stacktrace:");
77 Console.WriteLine (new System.Diagnostics.StackTrace (e));
81 public static void ThrowException (string message)
83 throw new Exception (message);
86 public static void ThrowException (ref string message)
88 throw new Exception (message);
91 public static void ThrowException (string message, int i)
94 ThrowException (message, --i);
96 throw new Exception (message);
99 public static void ThrowException (string message, out int o)
101 throw new Exception (message);
104 public static void ThrowExceptionGeneric<T> (string message)
106 throw new Exception (message);
109 public static void ThrowExceptionGeneric<T> (T a1)
111 throw new Exception ("Stack frame with generic method overload");
114 public static void ThrowExceptionGeneric<T> (List<string> a1)
116 throw new Exception ("Stack frame with generic method overload");
119 public static void ThrowExceptionGeneric<T> (List<T> a1)
121 throw new Exception ("Stack frame with generic method overload");
124 public static void ThrowExceptionGeneric<T1,T2> (string message)
126 throw new Exception (message);
130 public static void ThrowException (string message)
132 throw new Exception (message);
136 class InnerGenericClass<T> {
137 public static void ThrowException (string message)
139 throw new Exception (message);
142 public static void ThrowException (string message, T arg)
144 Console.WriteLine ("Generic to string:" + arg.ToString());
145 throw new Exception (message);
148 public static void ThrowException<T1> (string message, T1 arg)
150 throw new Exception (message);
153 public static void ThrowException<T1> (string message, T1 arg, InnerGenericClass<T1> _ignore)
155 throw new Exception (message as string);
158 public class InnerInnerGenericClass<T2> {
159 public static void ThrowException (T message)
161 throw new Exception (message as string);
164 public static void ThrowException (T2 message)
166 throw new Exception (message as string);
172 public static void Foo (int a, bool hard_crash) {
173 GenPair<T>.Foo<object> (a, hard_crash, new GenClass<T> (), new List<int> ());
178 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<int> zz) {
179 Foo<K,K,K> (a, hard_crash, null, null);
182 public static void Foo<K,J,F> (int a, bool hard_crash, GenClass<J> arg, List<int> zz) {
183 Foo<double> (a, hard_crash, null, new List<double> ());
186 public static void Foo<K> (int a, bool hard_crash, GenClass<T> arg, List<double> zz) {
187 ComplicatedTestCase.ArrayAndRef (a, new int[2], new int[2,2], ref hard_crash);
191 class ComplicatedTestCase {
192 public static int ArrayAndRef (int a, int[] b, int[,] c, ref bool hard_crash) {
194 for (int x = 0; x < a; ++x)
195 throw new Exception ("Stack trace with ambiguity");
199 public static void Foo (int a, bool hard_crash) {
200 GenClass<string>.Foo (a, hard_crash);
203 public static void Run () {