The following program prints 4096 in JIT mode and 1234 in AOT mode (for both legacy and netcore mono runtimes). The correct value is 4096 so it's something wrong with AOT.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
public class Program
{
public static void Main()
{
Console.WriteLine(SetValueDirect());
}
struct TestFields
{
public int MaxValue;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int SetValueDirect()
{
TestFields fields = new TestFields { MaxValue = 1234 };
FieldInfo info = typeof(TestFields).GetField("MaxValue");
TypedReference reference = __makeref(fields);
info.SetValueDirect(reference, 4096);
return fields.MaxValue;
}
}