# Why renaming survives source-generated COM


Open a .NET assembly that exports a classic `[ComVisible]` COM object in a decompiler and the method names are right there — they have to be, because a COM client can late-bind through `IDispatch` and ask for "Add" by name at runtime. Obfuscation and COM have an old, uneasy relationship for exactly this reason: the wire contract is partly made of your identifiers. So it is worth knowing that the modern .NET COM path breaks that link entirely. An interface marked `[GeneratedComInterface]` can have every one of its names renamed to a single letter and the native contract does not move.

## The real question

What actually travels across a COM boundary — the method *name*, or the method's *position*? The answer decides whether renaming is safe, and it is different for the two COM paths .NET gives you.

The classic path, `[ComVisible(true)]` plus a type library, carries names. A late-bound client calls `IDispatch::GetIDsOfNames("Add")` to turn the string into a dispatch id, then `Invoke`s it. Rename `Add` to `a` and that lookup fails. The source-generated path introduced in .NET 8, `[GeneratedComInterface]`, carries nothing but a GUID and an ordinal. It builds a classic COM vtable at compile time through `ComWrappers`, and dispatch is purely positional: the caller queries for the interface by its IID, gets a pointer to a table of function pointers, and calls the one at a fixed slot. No string is ever consulted. That single difference is why renaming is free on one path and fatal on the other.

## The mechanism, shown

Here is a minimal source-generated COM interface and its implementation. The source generator requires both to be `partial` and the interface to carry a `[Guid]`.

```csharp
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("5C13C3D8-4C4C-4F2B-9E1A-6B3A2F8E9D01")]
public partial interface ICalculator
{
    int Add(int a, int b);
    int Subtract(int a, int b);
}

[GeneratedComClass]
public partial class Calculator : ICalculator
{
    public int Add(int a, int b) => a + b;
    public int Subtract(int a, int b) => a - b;
}
```

`ComInterfaceGenerator` runs at compile time and emits the plumbing: a managed-to-unmanaged stub (so your managed code can *call* a native object through this interface) and an unmanaged-to-managed vtable (so native code can *call your* `Calculator`). The part that matters for obfuscation is how each side addresses a method. Here is the generated managed-to-unmanaged call for `Add`, abbreviated to its essence:

```csharp
// source-generated, abbreviated
int ICalculator.Add(int a, int b)
{
    var vtable = ((IUnmanagedVirtualMethodTableProvider)this)
        .GetVirtualMethodTableInfoForKey(typeof(ICalculator));
    int result;
    // slot 3 = the three IUnknown slots (0,1,2) + method index 0
    int hr = ((delegate* unmanaged<void*, int, int, int*, int>)vtable.Table[3])(
        vtable.ThisPtr, a, b, &result);
    Marshal.ThrowExceptionForHR(hr);
    return result;
}
```

The only identity on the wire is the IID — `typeof(ICalculator)` keys a table whose entry 3 is `Add`. The literal `3` is `Add`'s address. The string `"Add"` appears nowhere in the generated call; the native side on the other end of that function pointer does not know or care what the method is named. `Subtract` is slot 4, and so on.

That is the whole insight, drawn out:

<figure class="my-8"><svg viewBox="0 0 700 300" role="img" aria-label="Two COM paths compared. The [ComVisible] path publishes a type library mapping the name Add to a dispatch id, which renaming breaks. The [GeneratedComInterface] path builds a vtable where slot 3 is Add and slot 4 is Subtract, dispatched by position, so renaming Add to a and Subtract to b leaves the slots unchanged and the native contract intact." style="width:100%;height:auto"><defs><style>.box{fill:#eef4ff;stroke:#8eb4ff}.slot{fill:#ecfdf5;stroke:#6ee7b7}.bad{fill:#fef2f2;stroke:#fca5a5}.h{font:600 13px system-ui;fill:#0f172a}.t{font:12px system-ui;fill:#334155}.g{font:600 12px system-ui;fill:#059669}.r{font:600 12px system-ui;fill:#b91c1c}.s{font:12px ui-monospace,monospace;fill:#1e3a8a}</style></defs><text class="h" x="16" y="22">Classic [ComVisible] + type library — dispatch by name</text><rect class="bad" x="16" y="34" width="320" height="76" rx="8"/><text class="s" x="30" y="60">GetIDsOfNames("Add") -&gt; dispid 0x1</text><text class="s" x="30" y="82">Invoke(0x1, ...)</text><text class="r" x="30" y="102">rename Add to a : lookup fails</text><text class="h" x="16" y="150">.NET 8 [GeneratedComInterface] — dispatch by slot</text><rect class="box" x="16" y="162" width="200" height="118" rx="8"/><text class="t" x="30" y="186">vtable for IID 5C13C3D8...</text><rect class="slot" x="30" y="196" width="172" height="20" rx="4"/><text class="s" x="40" y="211">slot 0,1,2  IUnknown</text><rect class="slot" x="30" y="220" width="172" height="20" rx="4"/><text class="s" x="40" y="235">slot 3  Add(a,b)</text><rect class="slot" x="30" y="244" width="172" height="20" rx="4"/><text class="s" x="40" y="259">slot 4  Subtract(a,b)</text><rect class="box" x="360" y="162" width="324" height="118" rx="8"/><text class="t" x="374" y="186">after Nebula renaming</text><rect class="slot" x="374" y="196" width="296" height="20" rx="4"/><text class="s" x="384" y="211">slot 0,1,2  IUnknown</text><rect class="slot" x="374" y="220" width="296" height="20" rx="4"/><text class="s" x="384" y="235">slot 3  a(x,y)   &lt;- name changed, slot same</text><rect class="slot" x="374" y="244" width="296" height="20" rx="4"/><text class="s" x="384" y="259">slot 4  b(x,y)   &lt;- name changed, slot same</text><text class="g" x="360" y="294">IID + slot order preserved, so the contract holds</text></svg></figure>

Now obfuscate the assembly. Nebula renames `ICalculator` to `A`, `Calculator` to `B`, `Add` to `a`, `Subtract` to `b`. The source-generated stub and vtable are ordinary IL in the same module, so they are renamed in the same pass by the same map — the stub that was `ICalculator.Add` becomes `A.a`, still indexing `vtable.Table[3]`, still keyed by `typeof(A)`. The IID attribute blob is untouched: Nebula renames metadata identifiers, it does not rewrite custom-attribute values, so `[Guid("5C13C3D8-…")]` and the `[GeneratedComInterface]` marker ride through unchanged. A native host that was compiled against IID `5C13C3D8-…` queries for that IID, gets the vtable, calls slot 3, and runs `a` — the method formerly known as `Add`. Nothing on the native side sees the rename because the native side never saw a name.

## How it applies

The practical upshot is that source-generated COM is one of the few framework-integrated surfaces you can hand to the obfuscator with almost no keep-list. The names are genuinely internal. You do, however, have to protect three things the renamer does not touch but other transforms might.

**The IID.** The `[Guid]` on the interface is the COM contract identity. Nebula preserves attribute values by default, so this is safe unless you deliberately enable an attribute-stripping pass; if you do, scope it so it never removes `[Guid]` or `[GeneratedComInterface]`/`[GeneratedComClass]` from these types.

**Member order.** The vtable layout is positional, so reordering interface methods renumbers every slot after the change — a binary break against any already-compiled native caller. Renaming never reorders members, so default obfuscation is fine; the rule is simply never to let any member-reordering transform touch a `[GeneratedComInterface]`.

**Reachability.** This is the one case that needs a keep rule, and it is about *pruning*, not renaming. If `ICalculator` is `public`, `preservePublicApi` keeps it reachable and you need nothing. But if the interface is `internal` and is only ever called from native code across the COM boundary, the obfuscator's reachability analysis sees no managed caller and may treat `Add` and `Subtract` as dead methods eligible for removal. Pin them:

```json
{
  "schemaVersion": 1,
  "inputs": [ "bin/Release/net8.0/Acme.Interop.dll" ],
  "outputDirectory": "protected",

  "encryptStrings": true,
  "controlFlowObfuscation": true,

  "renaming": {
    "rules": [
      { "pattern": "Acme.Interop.*", "action": "rename" }
    ]
  },
  "pruning": {
    "rules": [
      { "pattern": "Acme.Interop.ICalculator", "action": "keep", "members": "all" },
      { "pattern": "Acme.Interop.Calculator",  "action": "keep", "members": "all" }
    ]
  }
}
```

Note what this does *not* do: it keeps the types from being pruned, but it leaves them in the `rename` ruleset, so their names still change. You are protecting reachability while still hiding every identifier — exactly the split that makes this surface cheap to obfuscate.

Verify it the way you verify any keep decision: open the protected assembly in [Glass.NET](https://delta1labs.com/products/glass/) and confirm the interface and class read as `A` and `B` with mangled members, that the `[Guid("5C13C3D8-…")]` attribute is intact on the interface, and that the two methods still exist in declaration order. Then run the native host against the protected build, because a COM contract break is invisible at compile time and obvious the instant the host calls a slot that moved.

## When it breaks

The free-renaming guarantee holds only for the source-generated, vtable-dispatched path. It evaporates the moment a name re-enters the contract.

If the same object is *also* exposed as `[ComVisible(true)]` for an `IDispatch` scripting client — an Office add-in, a VBScript host, an automation consumer — that client binds by name through `GetIDsOfNames`, and renaming the dispatched members breaks it. A type that straddles both worlds has to keep the names its `IDispatch` consumers use; see [obfuscating a COM-visible assembly](https://delta1labs.com/blog/obfuscate-com-visible-dotnet-assembly/) for that path's rules. Source-generated COM is IDispatch-free precisely to avoid this, so the clean case is an interface used *only* through `[GeneratedComInterface]`.

Two assemblies that both declare the same `[GeneratedComInterface]` to talk to each other must agree on the IID and the slot order, not the names — so you can obfuscate each with a different rename seed and they still interoperate, as long as neither reorders members and both keep the shared `[Guid]`. And if you marshal the interface across the boundary by its IID with a hand-written `QueryInterface`, that IID must match the attribute value you shipped; never let an attribute-stripping configuration remove it.

## Takeaway

Classic COM made your method names part of the wire contract, which is why obfuscating it has always meant a keep-list. The .NET 8 `[GeneratedComInterface]` path trades name-based `IDispatch` for a compile-time `ComWrappers` vtable, and a vtable is addressed by position, not by string. So the one thing obfuscation is best at — erasing identifiers — costs you nothing here: rename the interface, the class and every method, keep the IID, the member order and reachability, and the native contract never notices.

