Obfuscate

The Obfuscate aspect is used to control compiler-native code obfuscation throughout your project. It can be applied on project (assembly) level or to individual types or members. It can be applied in a nested fashion to override a more global setting, as well.

When applied on assembly level (e.g. with the assembly/__assembly prefix), obfuscation will be performed on every piece of code in the project according to the settings on the attribute (by default, obfuscation is off, so commonly this will be used to enable obfuscation for the whole project).

[assembly:Obfuscate]
[assembly:Obfuscate]
@__assembly:Obfuscate
@__assembly:Obfuscate

When applied to an individual type declaration, the aspect controls obfuscation for that type (and its members) only. Here, the aspect can be used to either mark individual types for obfuscation, or to exclude individual types from obfuscation, if obfuscation is enabled project-wide.

type
  [Obfuscate]
  SecretType = public class
    ...
  end;
[Obfuscate]
public class SecretType 
{
  ...
}
@Obfuscate
public class SecretType  {
  ...
}
@Obfuscate
public class SecretType
{
  ...
}

When applied to an individual member, the aspect controls obfuscation for that member only. Once again, the aspect can be used to either mark individual members for obfuscation, or to exclude* individual members from obfuscation, if obfuscation is enabled for the containing type or project-wide.

type
  SecretType = public class

    [Obfuscate]
    method SecretMethod();
  end;
public class SecretType 
{
    [Obfuscate]
    public void SecretMethod()
    {
        ...
    }
}
public class SecretType  {
  @Obfuscate
  func SecretMethod() {
        ...
    }
}
public class SecretType
{
    @Obfuscate
    public void SecretMethod()
    {
        ...
    }
}

Please also refer to the Compiler-Native Code Obfuscation topic, for more in-depth details.

Parameters

The Aspect has three optional boolean parameters:

  • Members controls whether type members will be obfuscated (true) or not (false). It defaults to true. If set to false, only type names will be obfuscated, but member names (e.g. methods and properties) will be left intact.
  • PublicMembers controls whether public members will be obfuscated (true) or not (false). If set to false (the default), only internal member names will be obfuscated; if set to true, public members will be renamed as well.
  • PublicTypes controls whether public types will be obfuscated (true) or not (false). If set to false (the default), only internal type names will be obfuscated; if set to true, public types will be renamed as well.
[Obfuscate(PublicMembers := true)]
[Obfuscate(PublicMembers = true)]
@Obfuscate(PublicMembers = true)
@Obfuscate(PublicMembers = true)

Note for class libraries: When obfuscating public members, the Elements compiler will emit metadata that will preserve the names of obfuscated members, so that projects that use the obfuscated library can still access all the obfuscated members transparently, using their real names.

This metadata is stored in the .fx file, so that the binary files (such as .dlls, .jars, or .a files) that will eventually be distributed to your end-users only contain the obfuscated names. When targeting .NET or Java (which does not emit .fx files by default), you will want to enable the Create .fx File option in project settings to enable this.

See Also