Created
September 14, 2023 05:48
-
-
Save mgravell/bf8908cb99e99bfda2e5513c02627efc 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
using System; | |
using System.Runtime.CompilerServices; | |
Bar b = CreateBar(42); // invoke a private .ctor | |
ref int x = ref GetBarX(b); // read field *ref* | |
Console.WriteLine(x); // deref field value | |
x = 17; // rewrite a private readonly field | |
Console.WriteLine(b.A); // 17 | |
[UnsafeAccessor(UnsafeAccessorKind.Constructor)] | |
static extern Bar CreateBar(int a); | |
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "x")] | |
static extern ref int GetBarX(Bar bar); | |
file class Bar | |
{ | |
private readonly int x; | |
public int A => x; | |
private Bar(int a) => x = a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment