# How Glass reads a vararg method


Most C# developers never type `__arglist`, and that is exactly why it is confusing when a decompiler shows it to you. You open a low-level interop shim in Glass.NET, and instead of a tidy `params` array you see a method whose signature trails off into `...` and a body full of `ArgIterator`. This is the CLR's real variadic calling convention — the same `vararg` convention C uses for `printf` — surfaced into C#. Reconstructing it is a good test of a decompiler, because the information a `params` method keeps in one place, a vararg method deliberately scatters.

## The real question

A `params int[]` method is ordinary. It takes a single array argument; the compiler allocates the array at the call site and the method reads it like any other array. Everything you need to decompile it is in the method definition.

A vararg method is not ordinary. Its definition records the fixed parameters and then a *sentinel* that says "and some number of extra arguments whose types I don't know." The types of those extra arguments are decided by the caller and recorded only in that **call site's** signature. So the question a decompiler must answer is: given a method whose definition is deliberately silent about its variadic tail, where does the type information actually live — and how do you get from raw IL back to `__arglist(2, 3, 4)`?

## The method definition keeps a secret

Start with the method. Here is a vararg sum in C#:

```csharp
using System;

static int SumAll(int first, __arglist)
{
    int total = first;
    var it = new ArgIterator(__arglist);
    while (it.GetRemainingCount() > 0)
        total += __refvalue(it.GetNextArg(), int);
    return total;
}
```

The `__arglist` in the parameter list is not a parameter you can name — it is the marker that makes this a vararg method. When Glass reads the method definition, the signature blob begins with the calling-convention byte `0x05` (`VARARG`) rather than the usual `0x00` (`DEFAULT`). ILDASM renders that as a `...` after the fixed parameters:

```il
.method private hidebysig static int32
        SumAll(int32 first,
               ...) cil managed
```

Note what is *not* here: any mention of `int`, or of three arguments, or of a count. The definition commits to exactly one fixed `int32` and then declares itself open. That is the secret — and it is why a decompiler cannot reconstruct the call just by reading the method.

## Reading the arguments: arglist and refanyval

The body is the other half. There is no array to index, so the method walks the incoming arguments with three special constructs that map one-to-one onto IL opcodes:

- `__arglist` (the expression) emits **`arglist`** (`0xFE 0x00`), which pushes a `RuntimeArgumentHandle` describing the extra arguments the caller passed.
- `__refvalue(tr, int)` emits **`refanyval`** (`0xC2`) to extract a managed pointer of a given type out of a `TypedReference`, followed by an `ldind` to load through it.
- Its siblings `__makeref` and `__reftype` emit `mkrefany` and `refanytype`.

The decompiled body comes straight off these:

```il
.locals init (int32 total, valuetype System.ArgIterator it)
  IL_0000: ldarg.0
  IL_0001: stloc.0
  IL_0002: ldloca.s   it
  IL_0004: arglist
  IL_0005: call       instance void System.ArgIterator::.ctor(valuetype System.RuntimeArgumentHandle)
  // loop:
  IL_000a: ldloca.s   it
  IL_000c: call       instance int32 System.ArgIterator::GetRemainingCount()
  IL_0011: brfalse.s  IL_0028
  IL_0013: ldloc.0
  IL_0014: ldloca.s   it
  IL_0016: call       instance valuetype System.TypedReference System.ArgIterator::GetNextArg()
  IL_001b: refanyval  [System.Runtime]System.Int32
  IL_0020: ldind.i4
  IL_0021: add
  IL_0022: stloc.0
  IL_0023: br.s       IL_000a
```

Glass recognises the `arglist` → `ArgIterator::.ctor` pairing and renders it as `new ArgIterator(__arglist)`, and the `refanyval T` + `ldind` pairing as `__refvalue(..., T)`. That is mechanical. The hard part is still upstream, at the caller.

## Where the types really live: the call site

Compile `SumAll(1, __arglist(2, 3, 4))` and the call looks like this:

```il
  IL_0000: ldc.i4.1
  IL_0001: ldc.i4.2
  IL_0002: ldc.i4.3
  IL_0003: ldc.i4.4
  IL_0004: call vararg int32 Program::SumAll(int32, ..., int32, int32, int32)
```

Read that call signature carefully. After the fixed `int32` comes a sentinel — ILDASM prints it as `...`, the raw blob encodes it as `ELEMENT_TYPE_SENTINEL` (`0x41`) — and *then* the three `int32` types of the arguments this particular call passed. Those three types are not in the method definition; they are in the signature blob attached to this `call` instruction, reached through its `MemberRef` (or `StandAloneSig`) token. A second call site elsewhere — `SumAll(9, __arglist(1.5, "x"))` — would carry a completely different tail (`float64, string`) after its own sentinel, against the very same method.

So Glass reconstructs a vararg call in two passes: it resolves the `call` token to find the method name and its fixed parameters, then it parses the call-site signature itself, splits it at the sentinel, and treats everything after the sentinel as the `__arglist(...)` arguments — matching each trailing type to the value pushed for it on the stack.

<figure class="my-8"><svg viewBox="0 0 760 320" role="img" aria-label="A vararg method definition stores only the fixed parameter and a sentinel; the variadic argument types live in each call site signature after the sentinel byte. Glass reads the method token for the name and fixed params, and the call-site signature for the arguments, then reconstructs the __arglist call." style="width:100%;height:auto;font-family:system-ui,sans-serif"><defs><marker id="va" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0 0l6 3-6 3z" fill="#64748b"/></marker></defs><text x="20" y="26" fill="#6366f1" font-size="13" font-weight="600">Method definition signature (VARARG 0x05)</text><rect x="20" y="38" width="130" height="46" rx="6" fill="#eef2ff" stroke="#6366f1"/><text x="85" y="60" text-anchor="middle" fill="#6366f1" font-size="12" font-weight="600">int32 first</text><text x="85" y="76" text-anchor="middle" fill="#64748b" font-size="10">fixed param</text><rect x="158" y="38" width="90" height="46" rx="6" fill="#fff7ed" stroke="#f59e0b"/><text x="203" y="60" text-anchor="middle" fill="#b45309" font-size="12" font-weight="600">SENTINEL</text><text x="203" y="76" text-anchor="middle" fill="#64748b" font-size="10">0x41  (...)</text><rect x="256" y="38" width="200" height="46" rx="6" fill="#fef2f2" stroke="#ef4444"/><text x="356" y="58" text-anchor="middle" fill="#dc2626" font-size="12" font-weight="600">no variadic types here</text><text x="356" y="75" text-anchor="middle" fill="#64748b" font-size="10">definition is silent on the tail</text><text x="20" y="128" fill="#6366f1" font-size="13" font-weight="600">Call-site signature: call vararg ... SumAll</text><rect x="20" y="140" width="110" height="44" rx="6" fill="#eef2ff" stroke="#6366f1"/><text x="75" y="166" text-anchor="middle" fill="#6366f1" font-size="12" font-weight="600">int32 first</text><rect x="138" y="140" width="84" height="44" rx="6" fill="#fff7ed" stroke="#f59e0b"/><text x="180" y="166" text-anchor="middle" fill="#b45309" font-size="12" font-weight="600">SENTINEL</text><rect x="230" y="140" width="70" height="44" rx="6" fill="#ecfeff" stroke="#0891b2"/><text x="265" y="166" text-anchor="middle" fill="#0891b2" font-size="12" font-weight="600">int32</text><rect x="308" y="140" width="70" height="44" rx="6" fill="#ecfeff" stroke="#0891b2"/><text x="343" y="166" text-anchor="middle" fill="#0891b2" font-size="12" font-weight="600">int32</text><rect x="386" y="140" width="70" height="44" rx="6" fill="#ecfeff" stroke="#0891b2"/><text x="421" y="166" text-anchor="middle" fill="#0891b2" font-size="12" font-weight="600">int32</text><text x="472" y="160" fill="#64748b" font-size="11">variadic arg types,</text><text x="472" y="176" fill="#64748b" font-size="11">per call site</text><rect x="20" y="214" width="240" height="56" rx="7" fill="#f0fdf4" stroke="#10b981"/><text x="140" y="238" text-anchor="middle" fill="#10b981" font-size="12" font-weight="600">Glass splits at the sentinel</text><text x="140" y="257" text-anchor="middle" fill="#64748b" font-size="10">fixed params | variadic tail</text><rect x="470" y="206" width="270" height="72" rx="7" fill="#f0fdf4" stroke="#10b981"/><text x="605" y="232" text-anchor="middle" fill="#10b981" font-size="13" font-weight="600">SumAll(1, __arglist(2, 3, 4))</text><text x="605" y="254" text-anchor="middle" fill="#64748b" font-size="11">reconstructed C#</text><path d="M300 162 L470 162" fill="none" stroke="#64748b" stroke-width="1.5" marker-end="url(#va)"/><path d="M260 242 L470 248" fill="none" stroke="#64748b" stroke-width="1.5" marker-end="url(#va)"/></svg><figcaption class="text-sm text-muted mt-2">The variadic argument types are absent from the method definition and present only in each call site's signature, after a sentinel byte; Glass splits the call signature there to rebuild the __arglist arguments.</figcaption></figure>

## Doing it from the CLI

You do not need the UI to see this. Point `glass export` at the assembly and ask for the member in IL, then in C#:

```console
$ glass export ./Interop.dll --member "M:Program.SumAll(System.Int32)" --il
$ glass export ./Interop.dll --member "M:Program.Main"
```

The first dumps the method body with the `arglist` and `refanyval` opcodes intact; the second shows the reconstructed caller, where Glass has already resolved the call-site signature and printed `SumAll(1, __arglist(2, 3, 4))`. Because vararg methods almost always live in interop code, this is the fast way to confirm what a shim actually forwards to a native entry point — all static, nothing executed.

## Caveats and where it gets hard

The honest limits are worth stating. A vararg method's documentation-comment ID only lists its *fixed* parameters, because that is all the definition has — `M:Program.SumAll(System.Int32)` names the method even though every call passes more. If you want to know what a given call passed, you must look at the call site, not the signature of the method you clicked.

`TypedReference`, `ArgIterator` and `RuntimeArgumentHandle` are *restricted* types: they cannot be boxed, stored in fields, or used as generic arguments, so the surrounding C# a decompiler can produce around them is constrained, and an aggressively optimised loop over them may not round-trip into the neat `while` shown above — Glass will fall back to showing the `ArgIterator` calls more literally rather than invent a `foreach` that the type system forbids. And vararg is specifically the *native* variadic convention; it is not the same thing as a `params` array, a `params Span<T>`, or a `params` collection, all of which are ordinary `DEFAULT`-convention methods that Glass reconstructs without any of this machinery.

## Takeaway

A vararg method hides its most interesting information from the place you would look first. The definition declares only the fixed parameters and a sentinel; the types of the variadic arguments live in every individual call site's signature, after an `ELEMENT_TYPE_SENTINEL` byte, and the body reads them through `arglist` and `refanyval`. Glass.NET reconstructs the method from its opcodes and the call from its call-site signature — resolving the token for the name and splitting the signature at the sentinel for the arguments — so you get `new ArgIterator(__arglist)` and `__arglist(2, 3, 4)` back from bytes that never spelled either out in one place. It is a small corner of the metadata, but it is a clean illustration of why decompiling well means reading more than the method you are looking at. Grab the free build from the [Glass.NET download page](https://delta1labs.com/products/glass) and point it at the next interop shim you need to understand.

