# Declarative Protection

The .NET Reactor user interface offers various ways to refine the level of protection applied to your application. The Advanced Rules editor allows you to select different inclusion and exclusion options, and you can also control the protection level using the standard .NET Obfuscation Attribute.

If you want .NET Reactor to respect this attribute, you must enable the Declarative Protection option in the settings:

Once you have enabled this option you can use the attribute to select specific protection features, choose whether to include or exclude those features, and specify the classes and members that should be affected.

# Selecting Protection Features

Protection features can be selected by assigning values to the attribute Feature property.

[System.Reflection.Obfuscation(Feature = "renaming,controlflow")

The following values are available:

  • "renaming or "obfuscation"
  • "stringencryption"
  • "controlflow"
  • "necrobit"
  • "antitamp"
  • "hidemethods"

Important: The values must be entered with the quotation marks and are case insensitive.

# Defining the behavior

Use the Exclude property to define whether the features should be included or excluded.

[System.Reflection.Obfuscation(Exclude = true, Feature = "renaming")
[System.Reflection.Obfuscation(Exclude = false, Feature = "controlflow")
  • To include features, set the Exclude property to false.

  • To exclude features, set the Exclude property to true. The default value is true, so explicitly setting the Exclude property is optional if you wish to exclude a feature.

# Placing the attribute and the 'ApplyToMembers' property

You can place the attribute on top of classes and members (methods, properties, and fields).

[System.Reflection.Obfuscation(Exclude = true, Feature = "controlflow")
internal void MyMethod()
[System.Reflection.Obfuscation(Exclude = false, Feature = "renaming", ApplyToMembers = false)
public class MyClass

The ApplyToMembers property has a default value of true. As it only affects whether class members (methods, properties, fields, and nested classes) are affected, it is only appropriate for attributes decorating a class.

If you do not want the class members to be affected, set the ApplyToMembers property to false.

# Examples

Example 1:

[System.Reflection.Obfuscation(Feature = "renaming")
class MyClass
{
   class NestedClass
   {}
   void MyMethod(){}    
}

MyClass, NestedClass, and MyMethod will not be renamed.

Example 2:

[System.Reflection.Obfuscation(Exclude = true, Feature = "renaming", ApplyToMembers = true)
class MyClass
{
   class NestedClass
   {}
   void MyMethod(){}    
}

This is equivalent to Example 1.

Example 3:

[System.Reflection.Obfuscation(Feature = "renaming", ApplyToMembers = false)]
class MyClass
{
   class NestedClass
   {}
   void MyMethod(){}    
}

Only MyClass is excluded from renaming, but not NestedClass and MyMethod.

Example 4:

[System.Reflection.Obfuscation(Exclude = false, Feature = "renaming,stringencryption") // include
[System.Reflection.Obfuscation(Exclude = true, Feature = "necrobit")  // exclude
class MyClass
{   
   int MyField;
   [System.Reflection.Obfuscation(Exclude = true, Feature = "renaming") // exclude
   void MyMethod(){}   
   [System.Reflection.Obfuscation(Exclude = false, Feature = "necrobit") // include
   void MyMethod2(){}  
}
  • Only MyField and MyMethod2 will be renamed.

  • String Encryption will be applied to MyMethod and MyMethod2

  • NecroBit will be only applied to MyMethod2.

# Special case: [System.Reflection.Obfuscation()]

There is a special case that works independently from "Declarative Protection". If you don't set a value for the Feature property, the attribute excludes renaming.

Example 5:

[System.Reflection.Obfuscation(ApplyToMembers = false)]
class MyClass
{
   class NestedClass
   {}
   void MyMethod(){}    
}

Only MyClass is excluded from renaming, but not NestedClass and MyMethod.

Example 6:

[System.Reflection.Obfuscation()]
enum MyEnum
{
    A,
    B,
    C
}

MyEnum and all fields are excluded from renaming.

Example 7:

enum MyEnum
{
    A,
    [System.Reflection.Obfuscation()]
    B,
    C
}

Only the B field is excluded from renaming.