Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / dynamic-method-finalize.2.cs
1 using System;
2 using System.Reflection;
3 using System.Reflection.Emit;
4
5 delegate int Getter ();
6
7 class Host {
8
9         static int Field = 42;
10
11         Getter g;
12
13         public Host (Getter g) {
14                 this.g = g;
15         }
16
17         ~Host () {
18                 int d = g ();
19                 Console.WriteLine (d);
20         }
21 }
22
23 class Program {
24
25         static Host h;
26
27         public static int Main ()
28         {
29                         DynamicMethod method = new DynamicMethod ("GetField",
30                         typeof (int), new Type [0], Type.GetType ("Host"));
31
32                         ILGenerator il = method.GetILGenerator ();
33                         il.Emit (OpCodes.Ldsfld, typeof (Host).GetField (
34                         "Field", BindingFlags.Static |
35 BindingFlags.NonPublic));
36                         il.Emit (OpCodes.Ret);
37
38                         Getter g = (Getter) method.CreateDelegate (typeof (Getter));
39
40                         /* 
41                          * Create an object whose finalizer calls a dynamic method which
42                          * dies at the same time.
43                          * Storing into a static guarantees that this is only finalized during
44                          * shutdown. This is needed since the !shutdown case still doesn't
45                          * work.
46                          */
47                         h = new Host (g);
48
49                         return 0;
50         }
51 }