How to exclude a class or member from obfuscation

When a plugin host, config file or serializer finds your code by name, mark it with the standard [Obfuscation(Exclude = true)] attribute and the name is kept. What is already skipped automatically, how to exclude one member or a whole class, and why to exclude as little as you can.

Most assemblies obfuscate cleanly with no configuration, because the cases that would break are skipped automatically. When one of yours is not — a class a plugin host finds by name, a field a serializer reads by string — you do not need a config file or a rules editor. You mark the code itself with a standard .NET attribute, and the name is kept.

What is already left alone

Before excluding anything by hand, check whether it is already covered. These keep their names without you doing anything:

  • The public API of a library. Public types and members of a DLL are a contract with callers and are preserved (why).
  • virtual and abstract members, so overrides and interface implementations still bind.
  • Constructors and Main.
  • Enums, generic types and [Serializable] types — skipped entirely.
  • Serialization contracts. A type marked [DataContract] keeps its name; a member marked [DataMember], [JsonInclude] or [JsonPropertyName] keeps its name.

That list is why the common case needs nothing. What it cannot know about is a lookup that exists only as a string somewhere else — in a config file, a database row, another process, or a call to Type.GetType("...").

Keeping one member's name

Add [Obfuscation(Exclude = true)] to the member. The attribute is System.Reflection.ObfuscationAttribute, part of .NET itself, so there is no package to reference and nothing of ours in your code:

using System.Reflection;

internal class ReportJob
{
    // Looked up by name from a scheduler's config file.
    [Obfuscation(Exclude = true)]
    internal void RunNightly() { /* ... */ }

    // Read by a serializer that matches on field name.
    [Obfuscation(Exclude = true)]
    private string tenantId;

    // Everything else in the class is renamed as usual.
    private void Cleanup() { /* ... */ }
}

It works on methods, fields, properties and events. The class itself is still renamed; only the marked members keep their names.

Keeping a whole class

Put the attribute on the type. By default it covers the type's name and every member inside it:

// A plugin host creates this by its full type name and calls its methods by name.
[Obfuscation(Exclude = true)]
internal class ExportPlugin
{
    internal void Initialize() { /* ... */ }
    internal void Export(string path) { /* ... */ }
}

If only the type name matters — something resolves the class by name but then calls it through an interface — keep the name and let the members be renamed:

[Obfuscation(Exclude = true, ApplyToMembers = false)]
internal class CsvImporter : IImporter
{
    private void ParseHeader() { /* still renamed */ }
}

What the attribute does not switch off

The exclusion is deliberately narrow: it is about names, because names are what break. It applies when the attribute's Feature is left at its default or set to "renaming". The other layers — string encryption, control-flow obfuscation, reference proxying — do not depend on what a member is called, so they still apply to excluded code. You keep the lookup working without leaving the method body unprotected.

The same attribute is also how you opt a method in to the strongest layer: [Obfuscation(Feature = "virtualization")] marks it for method virtualization on Pro.

Exclude as little as you can

Every excluded name is a signpost left standing. A reader who finds ValidateLicense intact has been told exactly where to look. So:

  • Exclude the member, not the class, when only one name is looked up.
  • Prefer fixing the lookup. nameof(RunNightly) is still a string after compilation, so it does not help — but replacing a name-based lookup with an interface, a delegate or a serializer attribute removes the need for the exclusion altogether. Obfuscation and reflection walks through each pattern.
  • Give excluded entry points dull names. If a host must find a method by name, Run gives away less than CheckTrialExpiry.

Finding out what needs excluding

Do not guess up front. Obfuscate, run the protected build, and let the failures tell you: a MissingMethodException, a TypeLoadException, or a value that silently comes back empty after deserialization each point at one name. On Pro, the rename map turns the obfuscated name in the error back into the original, so you know exactly which member to mark. The full routine is in how to test your obfuscated build before you ship.

Try it on your own assembly

Upload a DLL or EXE and download the obfuscated result. Free, no signup, nothing stored.