Skip to main content

How a decompiler restores explicit interface methods

Updated 9 min readMarkdown

Decompile a class that implements an interface explicitly and a naive tool shows you something that does not exist: a method called SomeNamespace.IFoo.Bar with a dot in the middle of its name, or worse, a plain private void Bar() that silently drops the interface it was written to satisfy. Neither compiles. The C# the author actually wrote — void IFoo.Bar() — is nowhere in the method's name, its signature, or its accessibility. It lives in a single metadata row that most readers never open. Recovering it is a clean, self-contained decompilation problem, and a good illustration of why a decompiler has to read metadata tables, not just method headers.

The real question

When you write public void Bar() to satisfy an interface, the runtime pairs your method to the interface slot by name and signature. Nothing explicit is needed. But void IFoo.Bar() is different on purpose: it is private, it does not appear through the class's own surface, and it exists precisely so a class can implement two interfaces that both declare Bar() with incompatible meanings. For that to work, the binding "this body fills that interface slot" cannot rely on the method's name — the name is deliberately hidden. So where does the binding live, and how does a decompiler find it to know that a private, oddly-named method is in fact an explicit interface implementation that must be printed with the IFoo. prefix?

What the compiler actually emits

Start from the source:

namespace MyApp
{
    public interface IGreeter { string Greet(); }

    public class ConsoleGreeter : IGreeter
    {
        string IGreeter.Greet() => "hello";
    }
}

The explicit implementation compiles to this method body:

.method private hidebysig newslot virtual final
        instance string 'MyApp.IGreeter.Greet'() cil managed
{
  .override method instance string MyApp.IGreeter::Greet()
  ldstr      "hello"
  ret
}

Read the header carefully, because every flag is doing work. The method is private — it is not callable as greeter.Greet(); you must go through an IGreeter reference. It is newslot virtual final: newslot gives it its own vtable slot (it does not override anything on a base class), virtual is mandatory for anything that fills an interface slot, and final seals it so no subclass can override it. The name is the mangled string MyApp.IGreeter.Greet — the interface's full name, a dot, and the member name — chosen so it can never collide with a public Greet on the same type. All of that is convention. None of it is what makes the method implement the interface.

The binding is the .override directive. In metadata it is a row in the MethodImpl table (ECMA-335 II.22.27), and it has exactly three columns:

  • Class — the type that owns the implementation, here ConsoleGreeter.
  • MethodBody — the method that provides the code, here MyApp.IGreeter.Greet.
  • MethodDeclaration — the interface method being implemented, here IGreeter::Greet.

That row is the entire fact. Delete it and keep the identical private method, and the class no longer implements IGreeter — the runtime has nothing pairing the body to the slot. Keep the row and the mangled name is irrelevant: you could rename the body to anything and the implementation would still bind. This is why the name is a convention a decompiler must not trust.

MethodDef (body)'MyApp.IGreeter.Greet'private newslot virtual finalname is mangled, not bindingInterface methodIGreeter::Greet()the slot to fillMethodImpl rowClass: ConsoleGreeterMethodBody → bodyMethodDeclaration → slotGlass reconstructsbody is referenced by aMethodImpl → not private helperstring IGreeter.Greet()
The mangled name and the private flag are decoys; the MethodImpl row is the only thing that makes the body an implementation of the interface slot.

The recovery heuristic

Glass builds the reconstruction the opposite way round from how a human reads the source. It does not scan method names for embedded dots. For every type, it loads the MethodImpl rows and indexes them by MethodBody. Then, for each method it is about to print:

  1. Is this method the MethodBody of any MethodImpl row? If no row references it, it is an ordinary method and is printed by its own name and accessibility — a private helper stays private void Foo(). This is the step a naive decompiler skips, and why it leaks private methods that were really interface implementations.
  2. Resolve the MethodDeclaration the row names. Follow the token to the method it points at and resolve its declaring type. If that type is an interface, this is an explicit interface implementation.
  3. Reconstruct the qualified name from the declaration, not the body. Glass takes the interface type (resolved through any TypeSpec, so a generic instantiation comes out as IEnumerable<int>) and the declared member name, and reprints the method as string IGreeter.Greet() — dropping the private, the virtual final newslot, and the mangled name, all of which C# supplies implicitly for this form.

The output is the line the developer wrote:

string IGreeter.Greet() => "hello";

The accessibility is gone because explicit implementations may not carry one; the mangled name is gone because the qualifier IGreeter. reproduces it exactly; the IL flags are gone because they are not expressible and the compiler re-derives them. Everything the reader needs is recovered from the MethodImpl target.

The cases that make it interesting

Two interfaces, one signature. This is the reason the feature exists at all. A type implements both IWalk and ISwim, and each declares void Move() with a different meaning:

public interface IWalk { void Move(); }
public interface ISwim { void Move(); }

public class Amphibian : IWalk, ISwim
{
    void IWalk.Move() { /* walk */ }
    void ISwim.Move() { /* swim */ }
}

Two private methods with identical signatures and mangled names (MyApp.IWalk.Move, MyApp.ISwim.Move), and two MethodImpl rows whose MethodDeclaration columns point at the two different interfaces. Glass keys off the declaration target, so it never confuses them: it emits void IWalk.Move() and void ISwim.Move() as distinct members. A tool that matched on the mangled name string would still work here by luck; one that matched on signature alone would collapse both into one.

Generic versus non-generic interface. The canonical case is a collection:

public class Numbers : IEnumerable<int>
{
    IEnumerator<int> IEnumerable<int>.GetEnumerator() => /* ... */;
    IEnumerator IEnumerable.GetEnumerator() => /* ... */;
}

Both GetEnumerator methods are explicit implementations with their own MethodImpl rows. The first row's MethodDeclaration is reached through a TypeSpec for IEnumerable<int> (a MethodSpec/MemberRef into the generic instantiation); the second targets the plain IEnumerable. Glass resolves the generic instantiation when it formats the qualifier, so the two come back as IEnumerator<int> IEnumerable<int>.GetEnumerator() and IEnumerator IEnumerable.GetEnumerator(). Trusting the mangled name here is risky: the generic one may be emitted as System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator, which is not valid C# to print verbatim — you have to resolve the type and re-render it in C# syntax.

Caveats and the honest limit

The mangled name is an implementation detail of the C# and VB compilers, and nothing in the CLR enforces it. An assembly from another language — or one that has been obfuscated — may bind an implementation through a MethodImpl while giving the body a name with no dot in it at all. This is exactly why Glass reads the MethodImpl table and treats the name as cosmetic: the reconstruction is correct even when the name convention is absent. The reverse also holds — a method literally named IFoo.Bar with no MethodImpl pointing at it is not an explicit implementation, and Glass will not invent one.

The one place reconstruction can only approximate is re-abstraction, where a class marks an inherited interface method back to abstract through a MethodImpl that points a slot at nothing a C# body can name. That is a runtime-level maneuver tied to interfaces with default implementations, and it has limited surface in ordinary C#; Glass shows the MethodImpl faithfully in the IL view and renders the nearest legal C# it can, rather than emitting source that would not compile. As always, compilable output wins over a prettier approximation.

Takeaway

void IFoo.Bar() is a fact stored in one row of the MethodImpl table, not in the method it describes. The body is a private, name-mangled, newslot virtual final slot that gives no reliable hint of its purpose, and the readable IFoo. qualifier is something a decompiler must reconstruct by following MethodBody back to the method and MethodDeclaration out to the interface. Glass.NET does exactly that, which is why it keeps two GetEnumerators apart, prints IWalk.Move and ISwim.Move as the separate members they are, and never leaks an interface implementation as a stray private method. Glass is a free .NET decompiler and assembly explorer — you can see both the MethodImpl rows in the IL view and the recovered C# side by side. The difference between the two readings is, once again, the difference between output that rebuilds and output that merely looks right.

No comments yet. Be the first to comment.