UnsafeAccessorAttribute Class


Provides access to an inaccessible member of a specific type.

public ref class UnsafeAccessorAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class UnsafeAccessorAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type UnsafeAccessorAttribute = class
    inherit Attribute
Public NotInheritable Class UnsafeAccessorAttribute
Inherits Attribute


public class Class
    static void StaticPrivateMethod() { }
    static int StaticPrivateField;
    Class(int i) { PrivateField = i; }
    void PrivateMethod() { }
    int PrivateField;

public void CallStaticPrivateMethod()

    [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = nameof(StaticPrivateMethod))]
    extern static void StaticPrivateMethod(Class c);
public void GetSetStaticPrivateField()
    ref int f = ref GetSetStaticPrivateField(null);

    [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "StaticPrivateField")]
    extern static ref int GetSetStaticPrivateField(Class c);
public void CallPrivateConstructor()
    Class c1 = PrivateCtor(1);

    Class c2 = (Class)RuntimeHelpers.GetUninitializedObject(typeof(Class));

    PrivateCtorAsMethod(c2, 2);

    extern static Class PrivateCtor(int i);

    [UnsafeAccessor(UnsafeAccessorKind.Method, Name = ".ctor")]
    extern static void PrivateCtorAsMethod(Class c, int i);

public void CallPrivateMethod(Class c)

    [UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(PrivateMethod))]
    extern static void PrivateMethod(Class c);
public void GetSetPrivateField(Class c)
    ref int f = ref GetSetPrivateField(c);

    [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "PrivateField")]
    extern static ref int GetSetPrivateField(Class c);


You can apply this attribute to an extern static method. The implementation of the extern static method annotated with this attribute will be provided by the runtime based on the information in the attribute and the signature of the method that the attribute is applied to. The runtime will try to find the matching method or field and forward the call to it. If the matching method or field is not found, the body of the extern static method will throw MissingFieldException or MissingMethodException.

For Method, StaticMethod, Field, and StaticField, the type of the first argument of the annotated extern static method identifies the owning type. Only the specific type defined will be examined for inaccessible members. The type hierarchy is not walked looking for a match.

The value of the first argument is treated as this pointer for instance fields and methods.

The first argument must be passed as ref for instance fields and methods on structs.

The value of the first argument is not used by the implementation for static fields and methods.

The return value for an accessor to a field can be ref if setting of the field is desired.

Constructors can be accessed using Constructor or Method.

The return type is considered for the signature match. Modreqs and modopts are initially not considered for the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match is attempted. If an ambiguity still exists, AmbiguousMatchException is thrown.

By default, the attributed method's name dictates the name of the method/field. This can cause confusion in some cases since language abstractions, like C# local functions, generate mangled IL names. The solution to this is to use the nameof mechanism and define the Name property.



Instantiates an UnsafeAccessorAttribute providing access to a member of kind UnsafeAccessorKind.



Gets the kind of member to which access is provided.


Gets or sets the name of the member to which access is provided.


When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)



Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)

Returns the hash code for this instance.

(Inherited from Attribute)

Gets the Type of the current instance.

(Inherited from Object)

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)

Creates a shallow copy of the current Object.

(Inherited from Object)

Returns a string that represents the current object.

(Inherited from Object)

Applies to