# Controlling obfuscation with ObfuscationAttribute


Rename everything, and the first type you serialize with `System.Text.Json` comes back as `{"a":1,"b":null}` instead of `{"Id":1,"Name":null}`. The first `Type.GetType("MyApp.PluginContract")` returns `null`. The first P/Invoke `[DllImport]` whose `EntryPoint` was inferred from the method name throws `EntryPointNotFoundException`. These are not obfuscator bugs — they are the obfuscator doing exactly its job on code that depends, somewhere, on a name staying the name.

The usual fix is a keep-list in the obfuscator config: a list of type and member patterns to exclude. It works, but it puts the decision in a file far away from the code that needs it, and it rots the moment someone renames a class. There is a cleaner mechanism, standardized in the BCL since .NET Framework 2.0 and almost nobody uses it well: `System.Reflection.ObfuscationAttribute`.

## The real question

How does a note you write in C# — `[Obfuscation(Exclude = true)]` — reach a tool that runs *after* the compiler, on the compiled assembly, and why does that note usually vanish from the thing you ship?

The answer is that the attribute is not code. It is data, written into the assembly's metadata in a form the obfuscator reads, and then — deliberately — erased.

## It's a custom attribute, nothing more

`ObfuscationAttribute` has no runtime behaviour. The CLR never consults it. It is a plain attribute class in the BCL with four properties:

```csharp
[AttributeUsage(/* assemblies, classes, methods, properties, fields, events, ... */,
                AllowMultiple = true, Inherited = false)]
public sealed class ObfuscationAttribute : Attribute
{
    public bool   Exclude              { get; set; } = true;
    public bool   ApplyToMembers       { get; set; } = true;
    public string Feature              { get; set; } = "all";
    public bool   StripAfterObfuscation { get; set; } = true;
}
```

Note the defaults, because they are the whole design. A bare `[Obfuscation]` means `Exclude = true`, `Feature = "all"`, `ApplyToMembers = true`, `StripAfterObfuscation = true` — "exclude this and its members from every transform, then remove this note from the output." You rarely write it bare; you set `Feature` to be specific.

When you compile, the C# compiler encodes this attribute into the `#Blob` heap and adds a `CustomAttribute` row pointing from your type (or method, or the assembly) to the `ObfuscationAttribute` constructor, with the named arguments `Feature` and `Exclude` packed into the blob. That is all `csc` does. The attribute is now a passive fact in your DLL.

## How the obfuscator reads it

An obfuscator opens the built assembly and, for every type and member, looks for a `CustomAttribute` whose type is `System.Reflection.ObfuscationAttribute`. It decodes the blob, reads `Feature` and `Exclude`, and uses them to decide whether each transform applies to that member. The figure traces one member from source to shipped output.

<figure class="my-8">
<svg viewBox="0 0 680 340" role="img" aria-label="A source attribute becomes a metadata custom-attribute blob; the obfuscator reads Feature and Exclude, skips the named transform for that member, then strips the attribute from the output" xmlns="http://www.w3.org/2000/svg" style="max-width:680px;width:100%;height:auto;font-family:system-ui,sans-serif;font-size:13px">
<text x="340" y="22" text-anchor="middle" fill="#64748b" font-weight="bold">From source note to shipped assembly</text>
<rect x="24" y="44" width="180" height="70" rx="6" fill="none" stroke="#6366f1" stroke-width="2"/>
<text x="114" y="68" text-anchor="middle" fill="#6366f1" font-weight="bold">1. C# source</text>
<text x="114" y="88" text-anchor="middle" fill="#64748b">[Obfuscation(</text>
<text x="114" y="104" text-anchor="middle" fill="#64748b">Feature="renaming")]</text>
<rect x="24" y="150" width="180" height="70" rx="6" fill="none" stroke="#0891b2" stroke-width="2"/>
<text x="114" y="174" text-anchor="middle" fill="#0891b2" font-weight="bold">2. Metadata</text>
<text x="114" y="194" text-anchor="middle" fill="#64748b">CustomAttribute row</text>
<text x="114" y="210" text-anchor="middle" fill="#64748b">+ #Blob bytes</text>
<rect x="250" y="150" width="180" height="70" rx="6" fill="#6366f1" fill-opacity="0.12" stroke="#6366f1" stroke-width="2"/>
<text x="340" y="174" text-anchor="middle" fill="#6366f1" font-weight="bold">3. Obfuscator reads</text>
<text x="340" y="194" text-anchor="middle" fill="#64748b">Feature="renaming"</text>
<text x="340" y="210" text-anchor="middle" fill="#64748b">Exclude=true</text>
<rect x="476" y="110" width="180" height="54" rx="6" fill="none" stroke="#10b981" stroke-width="2"/>
<text x="566" y="134" text-anchor="middle" fill="#10b981" font-weight="bold">4a. renaming</text>
<text x="566" y="152" text-anchor="middle" fill="#10b981">SKIPPED for member</text>
<rect x="476" y="176" width="180" height="54" rx="6" fill="none" stroke="#64748b" stroke-width="2" stroke-dasharray="5 4"/>
<text x="566" y="200" text-anchor="middle" fill="#64748b" font-weight="bold">4b. other transforms</text>
<text x="566" y="218" text-anchor="middle" fill="#64748b">still applied</text>
<rect x="250" y="256" width="406" height="58" rx="6" fill="none" stroke="#0891b2" stroke-width="2"/>
<text x="453" y="280" text-anchor="middle" fill="#0891b2" font-weight="bold">5. Shipped assembly</text>
<text x="453" y="300" text-anchor="middle" fill="#64748b">attribute STRIPPED (StripAfterObfuscation=true) — note is gone</text>
<line x1="114" y1="114" x2="114" y2="150" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<text x="150" y="136" fill="#64748b">csc</text>
<line x1="204" y1="185" x2="250" y2="185" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<line x1="430" y1="172" x2="476" y2="140" stroke="#10b981" stroke-width="1.5" marker-end="url(#ah)"/>
<line x1="430" y1="198" x2="476" y2="202" stroke="#64748b" stroke-width="1.5" stroke-dasharray="5 4" marker-end="url(#ah)"/>
<line x1="340" y1="220" x2="340" y2="256" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<defs><marker id="ah" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 Z" fill="#64748b"/></marker></defs>
</svg>
<figcaption class="text-sm text-muted mt-2">The attribute is compiled into the metadata blob, read by the obfuscator to skip the named <code>Feature</code> for that one member, and then removed from the output — so the shipped DLL carries the protection without carrying the instruction.</figcaption>
</figure>

## Per-member, per-feature

The point of `Feature` is that exclusion is not all-or-nothing. A serialization DTO must keep its property *names* so the JSON stays stable, but there is no reason its string literals should not be encrypted. Exclude only renaming:

```csharp
[Obfuscation(Feature = "renaming", Exclude = true)]
public sealed class CustomerDto
{
    public int    Id   { get; set; }
    public string Name { get; set; }
}
```

Nebula reads `Feature = "renaming"` and leaves the type and property names intact, while string encryption and any other transform still apply. The recognised Feature strings in Nebula are the transform names — `"renaming"`, `"string encryption"`, `"control flow"` — plus `"all"` for everything. Feature strings are **obfuscator-defined**: they are not in the .NET standard, so Nebula treats a Feature it does not recognise as a no-op rather than failing the build, and a project switching obfuscators must re-verify them.

`ApplyToMembers` controls the blast radius. On a type, `[Obfuscation(Feature = "renaming", Exclude = true, ApplyToMembers = true)]` cascades the renaming exclusion to every member of the type — every method, property, and field. Set `ApplyToMembers = false` and only the type name is kept while its members are still renamed. This cascade is exactly what you want on a plugin contract or a public API surface, and exactly what bites you if you put it on a large internal type by reflex.

You can also flip the sense. `Exclude = false` *includes* a member in a feature — useful when an assembly-wide default has turned something off and you want to force one member back in:

```csharp
// force control-flow obfuscation onto this one hot method,
// even if the assembly default is more conservative
[Obfuscation(Feature = "control flow", Exclude = false)]
private static bool VerifySignature(ReadOnlySpan<byte> sig) { /* ... */ }
```

## The assembly-level switch

`ObfuscateAssemblyAttribute` is the coarse companion. It sits on the assembly and declares intent for the whole module:

```csharp
// usually in AssemblyInfo.cs or a [assembly:] line
[assembly: ObfuscateAssembly(assemblyIsPrivate: true)]
```

`assemblyIsPrivate: true` tells the obfuscator this assembly is not a redistributable library with a public API others compile against — so it is free to rename public types too. Set it to `false` for a shared library and the obfuscator keeps the public surface renamable-safe. It also carries its own `StripAfterObfuscation` (default `true`).

## The caveat that matters most

Here is the trap. `StripAfterObfuscation` defaults to `true` for a reason: **the attribute documents your exclusions in plain text**. A row that says `ObfuscationAttribute(Feature="renaming") on class LicenseValidator` is a signpost — it tells anyone reading the metadata exactly which type you cared enough to protect from renaming, which is precisely the type they should look at first. Stripping removes the note from the output after it has been honoured, so the protection is applied but the instruction is gone.

If you set `StripAfterObfuscation = false` — or use an attribute the obfuscator does not strip — you ship that signpost. Open the protected DLL in any metadata viewer and the excluded members are listed for you. So the rule is simple: let it strip (the default), and only keep the attribute in the output in the rare case where a downstream tool genuinely needs to read it.

## When it breaks, and when to reach for the keep-list

Two honest limits.

First, **excluding is losing protection.** Every `[Obfuscation(Exclude = true)]` is a member that keeps its real name, and it is tempting to sprinkle them until the build stops throwing. Resist it. Exclude the minimum that the reflection, serialization, or interop boundary actually requires — the public contract and the names crossing it — not the implementation behind it. An assembly that excludes half its types is paying for an obfuscator and shipping a phone book.

Second, **the cascade is silent.** `ApplyToMembers = true` on a type with fifty members excludes fifty names, and nothing warns you. Audit the protected output: decompile it and confirm that the only names left standing are the ones you meant to keep.

Prefer the **attribute** when the decision is intrinsic to the member and must survive refactoring — a `[Serializable]` DTO, a reflection target resolved by name, a `[DllImport]` entry point. The note travels with the code; rename the class and the exclusion follows. Prefer the **config keep-list** when the decision belongs to the build rather than the source — excluding an entire vendored namespace you do not own, or a policy you would rather not compile into the shipped source at all. They compose: a keep-list draws the broad boundaries, the attribute handles the per-member exceptions the config cannot see. Basic renaming and exclusions, by either route, are available broadly in Nebula — you do not need the Pro tier to keep a name.

## Takeaway

`ObfuscationAttribute` is the rare piece of the BCL that exists only to talk to a tool that runs after the compiler. Treat it as a precise instrument: name the exact `Feature`, scope the cascade with `ApplyToMembers`, exclude as little as the boundary demands, and let `StripAfterObfuscation` erase the evidence on the way out. For the full set of transforms and how Nebula honours each Feature, see [Nebula.NET](https://delta1labs.com/products/nebula) and the [documentation](https://delta1labs.com/docs/nebula/) — and remember this is protection for your own IP, kept legible to your build and opaque to everyone else.

