Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created February 12, 2026 11:12
Show Gist options
  • Select an option

  • Save ufcpp/c27dcb71b277179a3b7e607bf9cb5af9 to your computer and use it in GitHub Desktop.

Select an option

Save ufcpp/c27dcb71b277179a3b7e607bf9cb5af9 to your computer and use it in GitHub Desktop.
拡張プロパティでフィールドへの UnsafeAccessor 書けそう。
using System.Runtime.CompilerServices;
var a = new A();
// a.Value() = 1; ではなく、() なしでフィールド参照。
a.Value = 1;
Console.WriteLine(a);
static class X
{
extension([UnsafeAccessorType("A")] object @this)
{
// 拡張プロパティでフィールドへの UnsafeAccessor 書けそう。
public extern ref int Value
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_value")]
get;
}
}
}
internal class A
{
private int _value;
public override string ToString() => $"A({_value})";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment