/
field-layout.cs
59 lines (57 loc) · 1.53 KB
/
field-layout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Runtime.InteropServices;
namespace Test {
[StructLayout ( LayoutKind.Explicit )]
public struct Doh {
[ FieldOffset(0) ] public int a;
[ FieldOffset(0) ] public byte a1;
[ FieldOffset(1) ] public byte a2;
[ FieldOffset(2) ] public byte a3;
[ FieldOffset(3) ] public byte a4;
}
[StructLayout ( LayoutKind.Explicit )]
public class Doh2 {
[ FieldOffset(0) ] public int a;
[ FieldOffset(0) ] public byte a1;
[ FieldOffset(1) ] public byte a2;
[ FieldOffset(2) ] public byte a3;
[ FieldOffset(3) ] public byte a4;
}
[StructLayout ( LayoutKind.Explicit )]
public class Doh3: Doh2 {
[ FieldOffset(0) ] public int b;
}
public class Test {
public static int Main() {
Doh doh;
Doh3 doh2 = new Doh3 ();
bool success = false;
// shut up the compiler
doh.a1 = doh.a2 = doh.a3 = doh.a4 = 0;
doh.a = 1;
if (doh.a1 == 1 && doh.a2 == 0 && doh.a3 == 0 && doh.a4 == 0) {
System.Console.WriteLine ("Little endian");
success = true;
} else if (doh.a1 == 0 && doh.a2 == 0 && doh.a3 == 0 && doh.a4 == 1) {
System.Console.WriteLine ("Big endian");
success = true;
}
if (!success)
return 1;
// shut up the compiler
doh2.a1 = doh2.a2 = doh2.a3 = doh2.a4 = 0;
doh2.a = 1;
if (doh2.a1 == 1 && doh2.a2 == 0 && doh2.a3 == 0 && doh2.a4 == 0) {
success = true;
} else if (doh2.a1 == 0 && doh2.a2 == 0 && doh2.a3 == 0 && doh2.a4 == 1) {
success = true;
}
doh2.b = 3;
if (doh2.a != 3)
success = false;
if (!success)
return 1;
return 0;
}
}
}