2 using System.Runtime.InteropServices;
6 [StructLayout(LayoutKind.Sequential, Size=32)]
7 public class TestStruct1
12 [StructLayout(LayoutKind.Sequential, Size=32)]
13 public class TestStruct2
15 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
20 [StructLayout(LayoutKind.Sequential, Size=32)]
21 public class TestStruct3 : TestStruct2
26 public unsafe static int Main ()
29 /// Testing simple struct size
31 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
36 TestStruct1 myStruct = new TestStruct1();
37 myStruct.a = 0x12345678;
39 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
40 Marshal.StructureToPtr(myStruct, p, false);
42 Type testType = typeof(TestStruct1);
43 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
46 Marshal.FreeHGlobal(p);
49 /// Testing struct size with ByValTStr string
51 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
54 TestStruct2 myStruct2 = new TestStruct2();
55 myStruct2.b = 0x12345678;
57 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
58 Marshal.StructureToPtr(myStruct2, p, false);
60 Type testType2 = typeof(TestStruct2);
61 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
64 Marshal.FreeHGlobal(p);
67 /// Test structure size and struct with inheritance
69 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
72 TestStruct3 myStruct3 = new TestStruct3();
73 myStruct3.b = 0x12345678;
74 myStruct3.c = 0x76543210;
75 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
76 Marshal.StructureToPtr(myStruct3, p, false);
78 Type testType3 = typeof(TestStruct3);
80 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
83 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c)
86 Marshal.FreeHGlobal(p);
89 /// Also make sure OffsetOf returns the correct Exception.
92 Marshal.OffsetOf(testType3, "blah");
95 catch(ArgumentException e) {