In corlib/System.Runtime.InteropServices:
[mono.git] / mono / tests / typeload-unaligned.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Z {
5     ~Z() {
6         Console.WriteLine("Hello, world!");
7     }
8 }
9
10 [StructLayout(LayoutKind.Explicit)]
11 public struct X {
12     [FieldOffset(0)] public short a;
13     [FieldOffset(2)] public Z z; // Unaligned reference
14 }
15
16 class Y {
17     static X test() {
18         X x = new X();
19         x.z = new Z();
20         return x;
21     }
22
23     static void test2(X x) {
24         Console.WriteLine("Object: " + x);
25     }
26
27     static int Main() {
28         try {
29                 X t1 = test();
30                 System.GC.Collect();
31                 System.GC.Collect();
32                 System.GC.WaitForPendingFinalizers();
33                 test2(t1);
34         } catch (TypeLoadException e) {
35                 Console.WriteLine ("got correct exception: {0}", e);
36                 return 0;
37         }
38         return 1;
39     }
40 }