|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +public class Test |
| 11 | +{ |
| 12 | + public struct WithORefs |
| 13 | + { |
| 14 | + public object F; |
| 15 | + } |
| 16 | + |
| 17 | + public struct WithNoORefs |
| 18 | + { |
| 19 | + public int F; |
| 20 | + } |
| 21 | + |
| 22 | + public ref struct WithByORefs |
| 23 | + { |
| 24 | + public ref int F; |
| 25 | + } |
| 26 | + |
| 27 | + [StructLayout(LayoutKind.Explicit)] |
| 28 | + public ref struct Explicit1 |
| 29 | + { |
| 30 | + [FieldOffset(0)] public Inner1 Field1; |
| 31 | + public ref struct Inner1 |
| 32 | + { |
| 33 | + public WithORefs Field2; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [StructLayout(LayoutKind.Explicit)] |
| 38 | + public ref struct Explicit2 |
| 39 | + { |
| 40 | + [FieldOffset(0)] public Inner2 Field1; |
| 41 | + public ref struct Inner2 |
| 42 | + { |
| 43 | + public WithNoORefs Field2; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [StructLayout(LayoutKind.Explicit)] |
| 48 | + public ref struct Explicit3 |
| 49 | + { |
| 50 | + [FieldOffset(0)] public Inner3 Field1; |
| 51 | + public ref struct Inner3 |
| 52 | + { |
| 53 | + public WithByORefs Field2; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public static void Validate_Explicit1() |
| 59 | + { |
| 60 | + Load1(); |
| 61 | + // that's enough. if we didn't throw a TypeLoadException, the test case will fail. |
| 62 | + |
| 63 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 64 | + static string Load1() |
| 65 | + { |
| 66 | + return typeof(Explicit1).ToString(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public static void Validate_Explicit2() |
| 72 | + { |
| 73 | + Load2(); |
| 74 | + // that's enough. if we didn't throw a TypeLoadException, the test case will fail. |
| 75 | + |
| 76 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 77 | + static string Load2() |
| 78 | + { |
| 79 | + return typeof(Explicit2).ToString(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public static void Validate_Explicit3() |
| 85 | + { |
| 86 | + Load3(); |
| 87 | + // that's enough. if we didn't throw a TypeLoadException, the test case will fail. |
| 88 | + |
| 89 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 90 | + static string Load3() |
| 91 | + { |
| 92 | + return typeof(Explicit3).ToString(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments