C# How to update a readonly field using UnsafeAccessor
Unsafe accessors can be used to access private members of a class, just like you would with reflection. And the same can be said about changing the value of a readonly field.
Let’s assume the following class:
class Foo
{
public readonly int readonlyField = 3;
}
Code language: C# (cs)
Let’s say that for some reason you want to change the value of that read-only field. You could already do that with reflection, of course:
var instance = new Foo();
typeof(Foo)
.GetField("readonlyField", BindingFlags.Instance | BindingFlags.Public)
.SetValue(instance, 42);
Console.WriteLine(instance.readonlyField); // 42
Code language: C# (cs)
But the same can be achieved using the UnsafeAccessorAttribute
without the performance penalty associated with reflection. Modifying read only fields is no different than modifying any other field when it comes to unsafe accessors.
var instance = new Foo();
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "readonlyField")]
extern static ref int ReadonlyField(Foo @this);
ReadonlyField(instance) = 42;
Console.WriteLine(instance.readonlyField); // 42
Code language: C# (cs)
This code is also available on GitHub in case you want to take it for a spin.