Dotfuscator can be configured by annotating your source code with .NET attributes.
Attributes provide a way to define protection settings directly in code rather than exclusively through a Dotfuscator configuration file. When Dotfuscator processes an assembly, it recognizes supported attributes and applies the corresponding protection settings to the annotated code elements. By default, Dotfuscator removes these attributes during processing so that the protected application does not contain protection configuration details.
Attributes are optional. Dotfuscator can also be configured entirely through a configuration file and the Config Editor.
When to Use Attributes
Attributes make it easier to see how specific code elements are protected by keeping protection settings close to the code they affect.
For example, the following example shows how the ObfuscationAttribute indicates a class should be excluded from renaming:
using System.Reflection;
[Obfuscation(Feature = "renaming", Exclude = true, ApplyToMembers = false)]
private class MyClass
{
// ...
}Use attributes when:
- Protection settings should be maintained alongside source code.
- Developers want protection requirements to be visible during development.
- Specific code elements require special protection settings.
When to Use the Config Editor Instead
Some scenarios are better suited to configuration through the Config Editor and configuration file:
- Protecting assemblies whose source code cannot be modified
- Frequently changing protection settings without recompiling
- Managing large groups of exclusions through rules
- Configuring checks that target multiple locations
You can combine attributes and configuration file settings within the same project. Dotfuscator applies both when processing assemblies. For instance, the snippet above indicates to Dotfuscator that MyClass should be excluded from renaming, but you can exclude other types using the Dotfuscator Config Editor and save those exclusions to the config file. When Dotfuscator processes the assemblies per the config file, it does not rename code items that were excluded either by attributes or the config file.
Supported Attribute Types
Dotfuscator recognizes the following attribute categories:
| Attribute Type | Purpose |
|---|---|
| Obfuscation Attributes | Configure obfuscation features through declarative obfuscation. |
| Check Attributes | Configure the injection of Checks into the protected application. |
Dotfuscator ignores attributes that it does not recognize.
Basic Workflow
The following workflow demonstrates how attributes are used during the protection process.
Step 1: Add Attribute References
Add the required attribute references to your project. The specific namespace and attribute depend on the protection feature you want to configure.
Step 2: Apply Attributes to Code
Annotate the assemblies, types, methods, properties, or fields that require special protection settings. The namespace (and thus the necessary using statement) varies depending on the attribute.
As with other attributes, you may omit the suffix Attribute in the usage. For instance, this is a usage of TamperCheckAttribute:
[TamperCheck(Action = CheckAction.Exit)]
public void MyMethod()
{
// method body
}Step 3: Build the Application
Compile the application as you normally would. At this stage, the attributes become part of the assembly metadata and are available for Dotfuscator to process.
Step 4: Use the Config Editor
Before Dotfuscator can process attributes, ensure the settings that correspond to the attribute types used by your application are enabled in the Config Editor:
| Setting | Description |
|---|---|
| Honor Obfuscation Attributes | Processes .NET Obfuscation Attributes found in the input assemblies. |
| Strip Obfuscation Attributes | Removes Obfuscation Attributes from the output assembly after processing. |
| Honor Check Attributes | Processes Dotfuscator Check Attributes found in the input assemblies. |
| Strip Check Attributes | Removes Check Attributes from the output assembly after processing. |
For example:
- If your application uses Obfuscation Attributes, enable Honor Obfuscation Attributes.
- If your application uses Check Attributes, enable Honor Check Attributes.
- To remove processed attributes from the protected assembly, enable the corresponding Strip options.
Step 5: Protect the Application
Run Dotfuscator using your preferred workflow:
- Config Editor
- MSBuild
- Command Line Interface
During processing, Dotfuscator applies the protections specified by the attributes.
Depending on the attribute configuration, Dotfuscator may also remove the attributes from the protected assembly.
Step 6: Test the Protected Application
After protection is applied, test the protected application to verify that:
- The application functions correctly.
- The expected protections have been applied.
- Any exclusions or special configurations behave as intended.