Created
May 7, 2026 15:09
-
-
Save ufcpp/e82a80953b85ec7d046e935670f65bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| partial struct MyUnion | |
| { | |
| public partial struct A; | |
| public partial struct B(int X, string S); | |
| public partial struct C(byte X, short Y, ref int R); | |
| public partial struct D(string S1, string S2); | |
| public partial struct E(string S, ref string R); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.ComponentModel; | |
| using System.Diagnostics; | |
| using System.Runtime.CompilerServices; | |
| using System.Runtime.InteropServices; | |
| [Union] | |
| ref partial struct MyUnion : IUnion | |
| { | |
| public ref partial struct A | |
| { | |
| private readonly object _0; | |
| private readonly object _1; | |
| private readonly ref byte __0; | |
| } | |
| public ref partial struct B | |
| { | |
| public int X = X; | |
| public string S = S; | |
| private readonly object _0; | |
| private readonly ref byte __0; | |
| } | |
| public ref partial struct C | |
| { | |
| public byte X = X; | |
| public short Y = Y; | |
| public ref int R = ref R; | |
| private readonly object _0; | |
| private readonly object _1; | |
| private readonly ref byte __0; | |
| } | |
| public ref partial struct D | |
| { | |
| public string S1 = S1; | |
| public string S2 = S2; | |
| private readonly ref byte __0; | |
| } | |
| public ref partial struct E | |
| { | |
| public string S = S; | |
| public ref string R = ref R; | |
| private readonly object _0; | |
| } | |
| [StructLayout(LayoutKind.Explicit)] | |
| private ref struct Union_ | |
| { | |
| [FieldOffset(0)] public A A; | |
| [FieldOffset(0)] public B B; | |
| [FieldOffset(0)] public C C; | |
| [FieldOffset(0)] public D D; | |
| [FieldOffset(0)] public E E; | |
| } | |
| private enum Kind_ : byte | |
| { | |
| A = 1, | |
| B, | |
| C, | |
| D, | |
| E, | |
| } | |
| private Union_ _union; | |
| private readonly Kind_ _kind; | |
| public MyUnion(A a) | |
| { | |
| _union.A = a; | |
| _kind = Kind_.A; | |
| } | |
| public MyUnion(B b) | |
| { | |
| _union.B = b; | |
| _kind = Kind_.B; | |
| } | |
| public MyUnion(C c) | |
| { | |
| _union.C = c; | |
| _kind = Kind_.C; | |
| } | |
| public MyUnion(D d) | |
| { | |
| _union.D = d; | |
| _kind = Kind_.D; | |
| } | |
| public MyUnion(E e) | |
| { | |
| _union.E = e; | |
| _kind = Kind_.E; | |
| } | |
| public readonly bool HasValue => _kind != 0; | |
| public readonly bool TryGetValue(out A value) | |
| { | |
| if (_kind == Kind_.A) | |
| { | |
| value = _union.A; | |
| return true; | |
| } | |
| value = default; | |
| return false; | |
| } | |
| public readonly bool TryGetValue(out B value) | |
| { | |
| if (_kind == Kind_.B) | |
| { | |
| value = _union.B; | |
| return true; | |
| } | |
| value = default; | |
| return false; | |
| } | |
| public readonly bool TryGetValue(out C value) | |
| { | |
| if (_kind == Kind_.C) | |
| { | |
| value = _union.C; | |
| return true; | |
| } | |
| value = default; | |
| return false; | |
| } | |
| public readonly bool TryGetValue(out D value) | |
| { | |
| if (_kind == Kind_.D) | |
| { | |
| value = _union.D; | |
| return true; | |
| } | |
| value = default; | |
| return false; | |
| } | |
| public readonly bool TryGetValue(out E value) | |
| { | |
| if (_kind == Kind_.E) | |
| { | |
| value = _union.E; | |
| return true; | |
| } | |
| value = default; | |
| return false; | |
| } | |
| [EditorBrowsable(EditorBrowsableState.Never)] | |
| [DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
| public readonly object? Value => throw new NotImplementedException(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment