DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Originally published at delta1labs.com

How Glass rebuilds a C# 14 extension block

C# 14 finally let you write list.IsEmpty and Point.Origin where IsEmpty and Origin are your additions to types you do not own. The classic this-parameter trick could never do that — it only ever added methods, because a property getter has no first parameter to mark. The new extension block closes the gap. And because the gap was in the language, not the runtime, the compiler had to invent a metadata shape to carry it. Decompiling that shape back into a readable block is a two-halves puzzle, and this post walks it for Glass.NET.

The source

A single extension block that adds an instance property, an instance method, and a static property to IEnumerable<T>:

public static class EnumerableExtensions
{
    extension<T>(IEnumerable<T> source)
    {
        public bool IsEmpty => !source.Any();

        public IEnumerable<T> WhereNotNull()
            => source.Where(x => x is not null);

        public static IEnumerable<T> Empty => [];
    }
}
Enter fullscreen mode Exit fullscreen mode

Three members, one receiver named source. Nothing here maps to a classic extension method except, loosely, WhereNotNull. IsEmpty is a property; Empty is a static member reached as EnumerableExtensions.Empty with no receiver at all. There is no this-parameter syntax that expresses any of this.

What the compiler emits

The receiver and the member signatures do not survive as C#. They survive as a nested type the compiler synthesises inside EnumerableExtensions, one per extension block, with an unspeakable name like <>E__0. That nested type is the skeleton. Its most important member is a marker method named <Extension>$ whose single parameter is the receiver:

.class nested public abstract sealed auto ansi beforefieldinit '<>E__0'<T>
{
    .method public hidebysig specialname static
        void '<Extension>$'(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!T> source) { }

    .property instance bool IsEmpty() { .get instance bool get_IsEmpty() }
    .method public hidebysig specialname instance bool get_IsEmpty() { ldnull throw }
    .method public hidebysig instance class IEnumerable`1<!T> WhereNotNull() { ldnull throw }
    .property class IEnumerable`1<!T> Empty() { .get static class IEnumerable`1<!T> get_Empty() }
    .method public hidebysig specialname static class IEnumerable`1<!T> get_Empty() { ldnull throw }
}
Enter fullscreen mode Exit fullscreen mode

Two things to notice. The receiver's name — source — is preserved, as the parameter name on <Extension>$. And every skeleton body is ldnull; throw. These methods never run; they exist only so the public shape of each member (name, signature, attributes, static-vs-instance) lives in metadata independently of the code that implements it.

The code that does run sits on the enclosing static class, under content-based mangled names, with the receiver spliced in as a real first parameter for the instance members:

.class public abstract sealed auto ansi beforefieldinit EnumerableExtensions
{
    .method public hidebysig static bool
        get_IsEmpty<T>(class IEnumerable`1<!T> source)
    {
        ldarg.0  call bool [System.Linq]System.Linq.Enumerable::Any<!!T>(...)
        ldc.i4.0  ceq  ret
    }
    .method public hidebysig static class IEnumerable`1<!T>
        WhereNotNull<T>(class IEnumerable`1<!T> source) { /* real body */ }
    .method public hidebysig static class IEnumerable`1<!T>
        get_Empty<T>() { /* real body, no receiver */ }
}
Enter fullscreen mode Exit fullscreen mode

The instance members (get_IsEmpty, WhereNotNull) take source as their first parameter. The static member (get_Empty) does not — a static extension member has no receiver, so its implementation is a plain generic static method. This asymmetry is the single most useful signal in the whole scheme: it is how a decompiler decides whether a member goes inside the block as static or as an instance member.

The pairing

Reconstruction is a join. For each skeleton member in <>E__0, Glass finds the matching implementation on the enclosing class and reads the body from there; it reads the shape — name, return type, static-or-instance, attributes — from the skeleton; and it reads the receiver once, from <Extension>$, to put source in scope for every instance body.

The skeleton carries each member's shape and the receiver; the enclosing class carries the bodies. Glass pairs them by signature and prints the block.

The static-vs-instance decision falls straight out of the implementation's parameter list. get_IsEmpty<T>(IEnumerable<T> source) has the receiver as its first parameter, so IsEmpty is an instance member and its body is printed with source in scope. get_Empty<T>() has no receiver, so Empty is static and nothing is in scope but the type parameter. Glass does not have to trust the skeleton's static flag alone — the implementation's shape confirms it.

The result is the block, reassembled verbatim:

public static class EnumerableExtensions
{
    extension<T>(IEnumerable<T> source)
    {
        public bool IsEmpty => !source.Any();
        public IEnumerable<T> WhereNotNull() => source.Where(x => x is not null);
        public static IEnumerable<T> Empty => [];
    }
}
Enter fullscreen mode Exit fullscreen mode

How you use this

Point Glass at any assembly that targets .NET 10 or later and uses extension blocks — your own libraries, a licensed dependency whose source you have lost, a package you are debugging an interop issue against. In the tree, the enclosing class shows its members; expand the synthesised <>E__0 type and Glass renders the recovered extension block rather than a bare nested type full of throwing stubs. The CLI and MCP surfaces give you the same reconstruction for scripted review:

glass decompile EnumerableExtensions.dll --type EnumerableExtensions
Enter fullscreen mode Exit fullscreen mode

If you only want to confirm the receiver and the member list without the bodies, the IL view shows the <Extension>$ marker method and the skeletons directly, which is the ground truth the C# view is built from.

When it breaks

The pairing is by signature, and signature collisions are where it frays. If two extension blocks in the same class declare members whose lowered implementations mangle to shapes a tool cannot tell apart, a decompiler can attach a body to the wrong skeleton; Glass keys the join on the full generic signature and the receiver type to avoid this, but adversarially mangled assemblies can still confuse any reader. A second limit is mixing: a class can hold both classic this-parameter extension methods and new extension blocks, and the two use different metadata — the [Extension] attribute for the old, the <>E__N skeleton for the new — so a faithful decompile shows each in its own form rather than forcing one into the other. Finally, the unspeakable names (<>E__0, <Extension>$) are compiler-internal and have changed across previews; Glass recognises the shape, not a hard-coded name, but a future lowering could shift the encoding and need a reader update.

Takeaway

An extension block is a compile-time grouping with no runtime existence, split at build time into a skeleton that remembers the shape and an implementation that carries the code. Rebuilding it is a join: read the receiver once from <Extension>$, pair each skeleton member to its implementation, and let the implementation's first parameter decide instance from static. Get that join right and an extension property — a thing the old syntax could never write — comes back exactly as it was written.

Top comments (0)