.NET 8 RC1 call method with UnsafeAccessorAttribute throw Internal CLR error
06:56 08 Oct 2023

I'm trying out a new .NET 8 feature to access a private field. Here's the code and project settings:

using System;
using System.Runtime.CompilerServices;

var t = new Test();
var size = Caller.GetSize(t);

Console.WriteLine(size);

public class Caller
{
    [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_size")]
    public static extern int GetSize(Test c);
}

public class Test
{
    private int _size = 4;
}


    
        Exe
        net8.0
    


But it throw exception:

Fatal error. Internal CLR error. (0x80131506)
   at Caller.GetSize(Test)
   at Program.
$(System.String[])

MSBuild 17

Any idea's?

I expected console output: 4

c# .net-8.0