Skip to main content
Version: 2023.4

6.2. How to Use Code Virtualization

To enable the code virtualization, you should apply a custom attribute to your method. In order to do that, you can use the instructions below.

Instructions on enabling code virtualization

  1. Open the source code of a method you want to virtualize

  2. Add a custom attribute as shown below (C#):

    using System;
    using System.Reflection;

    class YourClass
    {
    [Obfuscation(Feature = "virtualization", Exclude = false)]
    void YourMethod()
    {
    ...
    }
    }

    For Visual Basic .NET:

    Imports System
    Imports System.Reflection

    Class YourClass

    <Obfuscation(Feature:="virtualization", Exclude:=False)>
    Sub YourMethod()
    ...
    End Sub

    End Class
Note

Virtualization can significantly slow down the speed of code execution, so please use this feature wisely.

Applying Code Virtualization to Multiple Methods at Once

It may be beneficial to apply the code virtualization to the whole class or assembly. The conditional obfuscation can be employed to achieve that.

Examples are provided below.

Example 6.1. Virtualize all methods of a class

using System.Reflection;

[Obfuscation(Feature = "apply to member * when method or constructor: virtualization", Exclude = false)]
class YourClass
{
...
}

Example 6.2. Virtualize all methods in assembly

using System.Reflection;

[assembly: Obfuscation(Feature = "apply to type *: apply to member * when method or constructor: virtualization", Exclude = false)]