# Flattening namespaces during obfuscation


Open any protected .NET assembly in a decompiler and the type names are already gone — `a`, `b`, `c`, meaningless. Yet the tree on the left still reads like a floor plan: `Acme.Billing`, `Acme.Licensing`, `Acme.Core.Crypto`. The names are obfuscated; the *map* is not. Anyone skimming that tree knows exactly where your license validation lives before they read a single instruction. Flattening namespaces removes that map — and it works because of a fact most engineers never have reason to learn: a .NET namespace is not a container. It is a string.

## A namespace is not a node

In your source, `namespace Acme.Billing { class InvoiceService { } }` looks like it puts a class *inside* something. It does not. When the compiler emits metadata, there is no "namespace" record anywhere in the assembly — no namespace table, no parent node, nothing that owns the type. The `TypeDef` metadata table has one row per type, and each row carries two separate columns that both point into the string heap:

- **`Name`** — the simple type name, e.g. `InvoiceService`.
- **`Namespace`** — the dotted namespace *as a flat string*, e.g. `Acme.Billing`.

That second column is the entire namespace. It is a per-type prefix, stored independently on every type, and the dots in it are just characters — the runtime never splits `Acme.Billing` into `Acme` and `Billing`. The hierarchy you see in a decompiler or in Solution Explorer is reconstructed *by the tool*, after the fact, by grouping `TypeDef` rows that happen to share a `Namespace` string. There is no tree in the file. There is a flat table of types, each wearing a label.

<figure class="my-8"><svg viewBox="0 0 700 360" role="img" aria-label="The TypeDef metadata table before and after namespace flattening. Each row has separate Name and Namespace string-heap columns. Top-level types carry a dotted Namespace prefix; the nested type Line already has an empty Namespace. After flattening, every top-level type's Namespace column is cleared to the empty string, dropping the whole assembly into the global namespace, while the already-empty nested row is unchanged." style="width:100%;height:auto"><defs><style>.hd{font:600 13px system-ui;fill:#6366f1}.ch{font:600 11px system-ui;fill:#64748b}.cell{font:12px ui-monospace,monospace;fill:#0891b2}.ns{font:12px ui-monospace,monospace;fill:#10b981}.empty{font:italic 11px system-ui;fill:#64748b}.note{font:11px system-ui;fill:#64748b}.box{fill:none;stroke:#cbd5e1}.hl{fill:none;stroke:#6366f1;stroke-dasharray:3 2}</style></defs><text class="hd" fill="#6366f1" x="12" y="20">TypeDef table — BEFORE</text><text class="ch" fill="#64748b" x="16" y="44">Name</text><text class="ch" fill="#64748b" x="210" y="44">Namespace</text><rect class="box" x="12" y="52" width="330" height="92" rx="4"/><line x1="200" y1="52" x2="200" y2="144" class="box"/><line x1="12" y1="82" x2="342" y2="82" class="box"/><line x1="12" y1="112" x2="342" y2="112" class="box"/><text class="cell" fill="#0891b2" x="16" y="72">InvoiceService</text><text class="ns" fill="#10b981" x="210" y="72">Acme.Billing</text><text class="cell" fill="#0891b2" x="16" y="102">TaxEngine</text><text class="ns" fill="#10b981" x="210" y="102">Acme.Core</text><text class="cell" fill="#0891b2" x="28" y="132">Line  (nested)</text><text class="empty" fill="#64748b" x="210" y="132">"" (empty)</text><rect class="hl" x="204" y="118" width="132" height="22" rx="3"/><text class="note" fill="#64748b" x="12" y="170">Line is nested in InvoiceService — its namespace</text><text class="note" fill="#64748b" x="12" y="186">is implied via the NestedClass table, not stored.</text><text class="hd" fill="#6366f1" x="12" y="232">TypeDef table — AFTER flatten</text><text class="ch" fill="#64748b" x="16" y="256">Name</text><text class="ch" fill="#64748b" x="210" y="256">Namespace</text><rect class="box" x="12" y="264" width="330" height="92" rx="4"/><line x1="200" y1="264" x2="200" y2="356" class="box"/><line x1="12" y1="294" x2="342" y2="294" class="box"/><line x1="12" y1="324" x2="342" y2="324" class="box"/><text class="cell" fill="#0891b2" x="16" y="284">a</text><text class="empty" fill="#64748b" x="210" y="284">"" (cleared)</text><text class="cell" fill="#0891b2" x="16" y="314">b</text><text class="empty" fill="#64748b" x="210" y="314">"" (cleared)</text><text class="cell" fill="#0891b2" x="28" y="344">c  (nested)</text><text class="empty" fill="#64748b" x="210" y="344">"" (unchanged)</text><text class="hd" fill="#6366f1" x="392" y="20">What a decompiler shows</text><text class="note" fill="#64748b" x="392" y="46">Before: types group under</text><text class="ns" fill="#10b981" x="392" y="66">Acme.Billing, Acme.Core …</text><text class="note" fill="#64748b" x="392" y="86">— a readable architecture map.</text><text class="note" fill="#64748b" x="392" y="126">After: every top-level type sits</text><text class="note" fill="#64748b" x="392" y="146">in the global namespace. The</text><text class="note" fill="#64748b" x="392" y="166">grouping is gone because the</text><text class="note" fill="#64748b" x="392" y="186">strings it grouped on are gone.</text><text class="note" fill="#64748b" x="392" y="232">Only top-level rows change —</text><text class="note" fill="#64748b" x="392" y="252">nested types were already empty,</text><text class="note" fill="#64748b" x="392" y="272">so NestedClass still carries them</text><text class="note" fill="#64748b" x="392" y="292">under their (now flattened) parent.</text></svg><figcaption class="text-sm text-muted mt-2">A namespace is a per-type string in the TypeDef table's Namespace column — not a container. Flattening clears that column on top-level types; nested types are already empty.</figcaption></figure>

## The nested-type subtlety

Here is the detail that makes flattening safe and surgical. A **nested** type — `class Line` declared inside `InvoiceService` — does *not* store its namespace. Its `TypeDef` row has an **empty `Namespace` column**. Its place in the world is expressed entirely through a different table, `NestedClass`, which records a link from the nested `TypeDef` to its enclosing `TypeDef`. The runtime reconstructs the nested type's full name as *enclosing-full-name* + `+` + *nested-name* — for example `Acme.Billing.InvoiceService+Line`. The namespace is inherited from the parent; the nested row itself says nothing about it.

That means flattening never has to touch nested types at all. They already have an empty `Namespace`. You only ever rewrite the `Namespace` column of **top-level** types — and when you clear a parent's namespace, every type nested inside it follows automatically, because the nested type's effective namespace was always just its parent's.

## How Nebula flattens

Nebula.NET's renaming pass does exactly one thing for the namespace dimension, and it does it only on top-level types. After assigning a new opaque `Name`, it clears the `Namespace` column:

```csharp
// For each renamable type, after choosing the new short name:
t.Name = newName;
if (t.DeclaringType is null)      // top-level type only
    t.Namespace = string.Empty;   // drop it into the global namespace
// nested types (DeclaringType != null) are skipped:
// their Namespace is already empty and follows the parent.
```

`DeclaringType is null` is precisely the "is this a top-level type?" test. Nested types fall straight through, untouched, because their namespace lives in `NestedClass`, not on the row. The result is that every top-level type's `Namespace` becomes the empty string, and the whole assembly collapses into the global namespace. (If you prefer a single short bucket over the true global namespace — some tooling is happier with *one* non-empty namespace than with none — the same pass can instead write one short constant string into every top-level `Namespace` column. The mechanism is identical; only the value written differs.)

Consider what the reader loses. Before:

```csharp
namespace Acme.Licensing { internal sealed class LicenseValidator { bool Check(string key){ /* ... */ } } }
namespace Acme.Rendering  { internal sealed class PdfExporter    { void Export(Document d){ /* ... */ } } }
```

After renaming *and* flattening, a decompiler reconstructs something like:

```csharp
// global namespace — no Acme.Licensing, no Acme.Rendering
internal sealed class a { bool a(string a){ /* ... */ } }
internal sealed class b { void a(c a){ /* ... */ } }
```

Type renaming alone would still have shown `Acme.Licensing.a` and `Acme.Rendering.b` — the attacker would not know what `a` does, but they would know *which part of the system it belongs to*, which is often enough to find the licensing code in seconds. Flattening erases that grouping. It composes with renaming rather than replacing it: renaming hides what each type is called, flattening hides how the types were organised.

For the mechanics of the `Name` rewrite itself, see [choosing what to exclude from renaming in Nebula](https://delta1labs.com/blog/nebula-keep-list-rename-exclusions) and [what obfuscation breaks](https://delta1labs.com/blog/obfuscation-reflection-serialization-pitfalls); this post is about the *namespace* column specifically. For a tour of the `TypeDef`, `NestedClass`, and string-heap tables themselves, see [.NET metadata tables explained](https://delta1labs.com/blog/dotnet-metadata-tables-explained).

## When it breaks — and what to keep

Flattening is a namespace-level case of the one universal obfuscation failure: **something resolved a type by its full, string name at runtime.** Clear the `Namespace` column and every such lookup now asks for a prefix that no longer exists. The specific casualties:

**`Type.GetType` / `Assembly.GetType` by full name.** The most direct break:

```csharp
var t = Type.GetType("Acme.Licensing.LicenseValidator"); // → null after flattening
var t2 = asm.GetType("Acme.Rendering.PdfExporter");       // → null
```

The type is still in the assembly — it is just no longer called `Acme.Licensing.LicenseValidator`. Any plugin loader, factory, or config-driven instantiation that stores *full type names* as strings is affected.

**Serialization that embeds assembly-qualified type names.** `BinaryFormatter`-era payloads and JSON written with `TypeNameHandling` (Newtonsoft) or a polymorphic `$type` discriminator record the full type name on the wire. Flatten the namespace and previously serialized data can no longer bind back — the stored `Acme.Billing.Invoice` has no match. Round-tripping *new* data within one build is fine; reading *old* data, or data exchanged with an un-flattened peer, is not.

**WPF / XAML.** `clr-namespace:Acme.Controls` URIs, resource `pack://` URIs that reference a namespace, and `[assembly: XmlnsDefinition("...", "Acme.Controls")]` all resolve CLR namespaces by their string. Flattening breaks every one of them. (XAML apps generally want the fuller treatment in [obfuscating a WPF app](https://delta1labs.com/blog/obfuscate-wpf-app).)

**Anything else reflective over full names** — DI that scans by namespace, test discovery, `TypeNameHandling`-style messaging — fails for the same reason.

The fix is the keep-list, applied to the namespace dimension: keep the full name (`Name` *and* `Namespace`) of any type that is resolved by its string. In `nebula.config.json` the renaming ruleset carries it, and a `keep` preserves the namespace along with the name:

```json
{
  "renaming": {
    "rules": [
      { "pattern": "Acme.Plugins.Contracts.*", "action": "keep" },
      { "pattern": "Acme.*",                    "action": "rename" }
    ]
  }
}
```

The better move, where you control the format, is to stop depending on the CLR type name at all: give serialized polymorphic types a **stable discriminator** you choose (not the .NET type name), register plugins by an **explicit key** rather than `Type.GetType`, and pin XAML namespaces so markup does not chase a `clr-namespace`. Each dependency you convert from "find this type by its full name" to "find this type by a name I own" is one you can flatten freely.

Prove the result the same way you prove any keep-list: open the protected assembly in [Glass.NET](https://delta1labs.com/products/glass), confirm the `Namespace` column is empty on everything that should be hidden and intact on everything a loader resolves by name, then run your integration suite against the *flattened* build — a serialization round-trip or a `Type.GetType` path surfaces a missing keep the instant it executes.

## Takeaway

A namespace is a label, not a location — a per-type string in the `TypeDef` table's `Namespace` column, with nested types carrying an empty one and inheriting through `NestedClass`. Because it is only a string, [Nebula.NET](https://delta1labs.com/products/nebula) can erase your assembly's architectural map almost for free: clear the `Namespace` column on top-level types and the whole thing collapses into the global namespace, so renamed types no longer advertise which subsystem they came from. Keep the handful of full names that something resolves as a string, pin the rest to identities you control, and verify in a decompiler — then the map an attacker used to find your licensing code simply is not there.

