Skip to content
Discussion options

You must be logged in to vote

There is a feature planned for .NET 8 that introduces UnsafeAccessorAttribute that will allow access even to inaccessible members of a type using extern methods, e.g. you can call a private method or get access to a private field without using reflection.
This is also done at compile time so the overhead is minimal

Here is the proposal
dotnet/runtime#81741

Here is the impl
dotnet/runtime#86161

Example:

using System.Runtime.CompilerServices;

class MyClass
{
    private string GetName() => "John";
}

[UnsafeAccessor(UnsafeAccessorKind.Method)]
extern static string GetName(MyClass myClass);

MyClass myClass = new();
string name = GetName(myClass); // John

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TonyValenti
Comment options

Answer selected by wrexbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants