[interp] Remove unreachable code (#12411)
[mono-project.git] / mono / tests / delegate-invoke.cs
blob692c02e073b6d835360f97c5a10a6d8e0f82ecf0
1 using System;
2 using System.Reflection;
3 using System.Globalization;
4 using System.Diagnostics;
6 struct EmptyStruct {
7 public int value;
9 public int test() {
10 return value + 10;
13 public static int test2 (ref EmptyStruct foo) {
14 return foo.value + 20;
18 delegate int ActionRef (ref EmptyStruct _this);
19 delegate int ActionRef2 ();
20 delegate int ActionRef3 (EmptyStruct _this);
22 public struct Foo {
23 public int x,y,z;
24 public Foo (int i) {
25 x = y = z = i;
29 delegate Foo NoArgDele ();
30 delegate Foo OneArgDele (Driver d);
33 public class Driver
35 public static Foo M0 () { return new Foo (10); }
36 public Foo M1 () { return new Foo (this == null ? 10 : 20); }
37 public static Foo M2 (string x) { return new Foo (30); }
38 public static Foo M2i (int x) { return new Foo (40); }
39 public static Foo M3 (object x) { return new Foo (50); }
40 public virtual Foo M4 () { return new Foo (this == null ? 60 : 70); }
42 public static int Main () {
43 int r = test_0_refobj_invokes ();
44 if (r != 0)
45 return r;
46 r = test_0_valuetype_invokes ();
47 if (r != 0)
48 return r + 20;
49 return 0;
52 public static int test_0_refobj_invokes ()
54 NoArgDele x;
55 OneArgDele y;
57 x = Driver.M0;
58 if (x ().x != 10)
59 return 1;
61 x = new Driver ().M1;
62 if (x ().x != 20)
63 return 2;
65 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), "10", typeof (Driver).GetMethod ("M2"));
66 if (x ().x != 30)
67 return 3;
69 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M3"));
70 if (x ().x != 50)
71 return 4;
73 y = (OneArgDele)Delegate.CreateDelegate (typeof (OneArgDele), null, typeof (Driver).GetMethod ("M4"));
74 if (y (new Driver ()).x != 70)
75 return 5;
77 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M1"));
78 if (x ().x != 10)
79 return 6;
81 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M4"));
82 if (x ().x != 60)
83 return 7;
85 return 0;
88 public static int test_0_valuetype_invokes ()
90 EmptyStruct es = default (EmptyStruct);
91 es.value = 100;
93 var ar1 = (ActionRef)Delegate.CreateDelegate(typeof (ActionRef), typeof (EmptyStruct).GetMethod("test"));
94 if (ar1 (ref es) != 110) {
95 Console.WriteLine ("expected 110, got {0}", ar1 (ref es));
96 return 1;
99 var ar2 = (ActionRef2)Delegate.CreateDelegate (typeof (ActionRef2), null, typeof (EmptyStruct).GetMethod("test"));
100 try {
101 Console.WriteLine ("must not return, got {0}", ar2 ());
102 return 2;
103 } catch (NullReferenceException) {
106 ar1 = (ActionRef) Delegate.CreateDelegate(typeof (ActionRef), typeof (EmptyStruct).GetMethod("test2"));
107 if (ar1 (ref es) != 120) {
108 Console.WriteLine ("expected 120, got {0}", ar1 (ref es));
109 return 3;
112 ar2 = (ActionRef2) Delegate.CreateDelegate(typeof (ActionRef2), es, typeof (EmptyStruct).GetMethod("test"));
113 if (ar2 () != 110) {
114 Console.WriteLine ("expected 110 got {0}", ar2 ());
115 return 4;
118 try {
119 Delegate.CreateDelegate(typeof (ActionRef2), new EmptyStruct (), typeof (EmptyStruct).GetMethod("test2"));
120 Console.WriteLine ("must fail/2");
121 return 5;
122 } catch (ArgumentException) {}
124 try {
125 Delegate.CreateDelegate(typeof (ActionRef3), typeof (EmptyStruct).GetMethod("test"));
126 Console.WriteLine ("must fail/2");
127 return 6;
128 } catch (ArgumentException) {}
130 return 0;