Skip to content
← All posts
· Delta1 Labs UnsafeAccessorGlass

Decompiling [UnsafeAccessor]: the extern method with no body and a private name in its attribute

An UnsafeAccessor method has no IL body at all — the private member it reaches lives only as a string in a custom attribute blob, bound by the JIT. Here is how Glass.NET reads it back.

[UnsafeAccessor], added in .NET 8, is the sanctioned way to reach a non-public member without reflection. You declare a static extern method, decorate it with the kind of access you want and the name of the member, and the runtime binds it to the real field, method or constructor at JIT time — no MethodInfo, no delegate, no boxing, and the call site is an ordinary direct call. It is how serializers, test harnesses and parts of the BCL itself reach internals they are entitled to touch without paying the reflection tax.

It is also one of the stranger things a decompiler has to read back, because the method it has to reconstruct has no body at all, and the one piece of information that gives it meaning — the name of the private member — lives nowhere except a string inside a custom-attribute blob. A decompiler that walks IL instructions finds nothing; a decompiler that walks the member-reference table finds no link to the target. Everything that matters is in metadata the JIT reads and the instruction stream never mentions.

This post reads the raw metadata for a field accessor, a method accessor and a constructor accessor, shows exactly where the private name is stored, and walks through how Glass.NET turns each one back into the static extern declaration the author wrote.

1 · the method body — empty.method static string& GetSecret( class UserClass u) cil managedRVA 0 · no .locals · no instructions2 · the attribute blob — the only linkUnsafeAccessorAttribute(Field) Name = "_secret"a UTF-8 string — not a FieldRef token3 · the call site — ordinaryldloc.0 // UserClasscall string& GetSecret(class UserClass)no mention of _secret anywhere herewhat Glass reconstructs[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_secret")]static extern ref string GetSecret(UserClass u);The JIT reads 1 + 2, finds _secret on the type of the first parameter, checks the signatures, and binds a direct accessor.Drop the attribute and the method is an empty extern that throws at runtime; drop the Name and the binding target is gone.

A field accessor is a bodyless method returning a byref

Start with the smallest possible example: a type with a private field, and an accessor that returns a ref to it.

public class UserClass
{
    private string _secret = "hidden";
}

public static class Accessors
{
    [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_secret")]
    public static extern ref string GetSecret(UserClass u);
}

GetSecret has no body you ever wrote, and the compiler emits none. In IL it is a managed method whose entire definition is the signature and the attribute:

.method public hidebysig static
        string& GetSecret (class UserClass u) cil managed
{
  .custom instance void [System.Runtime]System.Runtime.CompilerServices.UnsafeAccessorAttribute::.ctor(
          valuetype [System.Runtime]System.Runtime.CompilerServices.UnsafeAccessorKind)
    = ( 01 00 03 00 00 00 01 00 54 0E 04 4E 61 6D 65 07 5F 73 65 63 72 65 74 )
} // end of method Accessors::GetSecret

There is no .maxstack, no .locals, no instruction — the method’s RVA is 0. That is what static extern compiles to: a managed method the runtime is expected to supply a body for. For P/Invoke the runtime supplies a marshalling stub; for [UnsafeAccessor] it supplies a direct-access thunk. The implementation is not pinvokeimpl and there is no ImplMap row — this is the signal that separates it from [DllImport], which is the other thing a bodyless managed extern method can be.

The return type is string& — ELEMENT_TYPE_BYREF string. A field accessor returns a managed reference to the field, which is what lets the caller both read and write it; ref string in C# is string& in IL. A decompiler that reads the byref and renders ref string is right; one that strips it to string produces a read-only copy accessor that no longer aliases the field.

The private name is in the blob, and only there

Decode that attribute blob byte by byte, because it is the only place _secret appears:

01 00                      ; prolog (custom-attribute blob version)
03 00 00 00                ; ctor arg: UnsafeAccessorKind = Field (3)
01 00                      ; number of named arguments = 1
54                         ; named arg is a PROPERTY (0x54)
0E                         ; property type = ELEMENT_TYPE_STRING
04 4E 61 6D 65             ; property name  : length 4, "Name"
07 5F 73 65 63 72 65 74    ; property value : length 7, "_secret"

UnsafeAccessorKind is an enum, so its ctor argument is serialized as its underlying int32: 03 00 00 00 is Field. The Name is a serialized string — a length prefix and raw UTF-8 bytes. Nowhere in the method, and nowhere else in the module’s metadata, is there a FieldRef or MemberRef token pointing at UserClass._secret. The binding is by string, resolved by the JIT against the type of the first parameter. For a decompiler this is the crux: the member name must be read out of the serialized blob and placed back into the regenerated attribute, because the call graph, the member-reference table and the instruction stream all say the accessor references nothing.

A method accessor: the first parameter is the receiver

Reaching a private method is the same shape, with the target’s own parameters appended after the instance:

public class UserClass
{
    private int Compute(int x, int y) => x * y + 1;
}

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "Compute")]
public static extern int CallCompute(UserClass u, int x, int y);
.method public hidebysig static
        int32 CallCompute (class UserClass u, int32 x, int32 y) cil managed
{
  .custom instance void [...]UnsafeAccessorAttribute::.ctor(valuetype [...]UnsafeAccessorKind)
    = ( 01 00 01 00 00 00 01 00 54 0E 04 4E 61 6D 65 07 43 6F 6D 70 75 74 65 )
}

The kind is now 01 00 00 00 — Method — and the Name value is 43 6F 6D 70 75 74 65, "Compute". The convention the runtime enforces is that for Method and Field the first parameter is the instance whose member is accessed, and the remaining parameters must match the target method’s signature. That convention is structural, not syntactic: there is no keyword to reconstruct, so Glass renders the parameters exactly as they appear. What it must get right is that CallCompute’s signature after the receiver — (int32 x, int32 y) → int32 — is the signature the attribute binds against, so the regenerated declaration has to preserve parameter and return types precisely or the runtime binding would fail on recompile.

A call to it is unremarkable:

ldloc.0            // UserClass u
ldc.i4.6
ldc.i4.7
call int32 Accessors::CallCompute(class UserClass, int32, int32)

Again: the call token names CallCompute, never Compute. The private method’s name survives only in the callee’s attribute.

A constructor accessor: the name is the return type

For UnsafeAccessorKind.Constructor the shape inverts — there is no instance parameter, the accessor’s return type is the type being constructed, and Name is not used (a constructor has no member name of its own):

public class UserClass
{
    private UserClass(string seed) { _secret = seed; }
}

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
public static extern UserClass CreateUser(string seed);
.method public hidebysig static
        class UserClass CreateUser (string seed) cil managed
{
  .custom instance void [...]UnsafeAccessorAttribute::.ctor(valuetype [...]UnsafeAccessorKind)
    = ( 01 00 00 00 00 00 00 00 )
}

The blob is now just the kind — 00 00 00 00 for Constructor — followed by 00 00, zero named arguments. There is no Name, because the runtime finds the constructor by matching the parameter list against the type named in the return. Glass reads the kind as Constructor, sees the empty named-argument count, and deliberately emits no Name = — adding one would be wrong, and some runtimes reject a named constructor accessor. The target type comes from the return signature class UserClass, which the reconstruction keeps as the method’s return type.

UnsafeAccessorKind.StaticMethod and StaticField round out the set: they drop the instance parameter (the target is static), and their blobs carry kind 02 and 04 respectively with a Name. .NET 9 extended the feature to generic targets, so you will also meet accessors with type parameters and a matching generic signature; the reconstruction rule is unchanged — read the kind and the Name from the blob, keep the signature verbatim.

Reading one back with Glass

Because the interesting part is a custom attribute and an empty body, the IL view is the fastest way to confirm what an accessor actually binds to. Point Glass at the assembly and dump the single member:

$ glass export ./Accessors.dll --member "M:Accessors.GetSecret(UserClass)" --il

That prints the bodyless method and the decoded UnsafeAccessorAttribute, so you can see the kind and the Name without decoding the blob by hand. Drop --il and Glass reconstructs the C#:

$ glass export ./Accessors.dll --member "M:Accessors.GetSecret(UserClass)"
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_secret")]
public static extern ref string GetSecret(UserClass u);

The declaration is the whole method — no body, the ref preserved, the private name pulled out of the blob and put back where the author wrote it. Glass uses the same ICSharpCode.Decompiler engine the UI does, so you get the identical result in the side-by-side view when you open the assembly in the app and select the accessor. Everything here is static: Glass reads the metadata and reconstructs the C#, and it never JITs the accessor or runs the target type — which is exactly why it is safe to point at a dependency you are still deciding whether to trust.

Understanding how [UnsafeAccessor] decompiles is increasingly practical, because the pattern is spreading from the BCL into mainstream libraries that want reflection-free access to internals they own. When you are debugging your own accessor that silently binds to the wrong overload, or reading how a serializer reaches a private setter, the answer is never in the method body — there isn’t one. It is in the attribute blob and the signature, and that is precisely what Glass surfaces. Grab the latest build from the Glass.NET download page and read the next bodyless extern you meet.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.