Skip to content
← All posts
· Delta1 Labs Nebula.NETObfuscation

Controlling obfuscation with ObfuscationAttribute

How [Obfuscation(Exclude=..., Feature=..., ApplyToMembers=...)] and [ObfuscateAssembly] let a .NET engineer mark individual types and members in source — read from the custom-attribute metadata blob — and why StripAfterObfuscation decides whether that instruction survives into the shipped assembly.

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:

[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.

From source note to shipped assembly 1. C# source [Obfuscation( Feature="renaming")] 2. Metadata CustomAttribute row + #Blob bytes 3. Obfuscator reads Feature="renaming" Exclude=true 4a. renaming SKIPPED for member 4b. other transforms still applied 5. Shipped assembly attribute STRIPPED (StripAfterObfuscation=true) — note is gone csc
The attribute is compiled into the metadata blob, read by the obfuscator to skip the named Feature for that one member, and then removed from the output — so the shipped DLL carries the protection without carrying the instruction.

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:

[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:

// 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:

// 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 and the documentation — and remember this is protection for your own IP, kept legible to your build and opaque to everyone else.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.