Share via


Transforming Calls to Virtual Methods of an Enumeration

Microsoft Intermediate Language (MSIL) code containing non virtual calls, using the call instruction, to virtual methods of an enumeration will be transformed to incorrect Secure Virtual Machine (SVM) code. Executing such transformed code will result in a System.Reflection.TargetException runtime exception, with a message Object does not match target type

The problem occurs if this method is compiled by using the Microsoft .NET Framework 1.1, and is then transformed by using the Microsoft SLP Code Protector. However, the problem will not occur if the method is compiled using the Microsoft .NET Framework 2.0, because the IL generated for this method by this compiler uses the callvirt instruction, rather than the call instruction. 

 

Affected Methods 

Enum.ToString() 

Enum.GetHashCode() 

Enum.Equals() 

 

For example, the following C# method call (shown before transformation) will result in the exception mentioned. However, by substituting the work around shown in the code comment for string result = myVal.ToString(); the exception will not occur. 

 

Copy Code

  
    private
   static void TestEnum()
{

    System.Windows.Forms.AccessibleEvents myVal =
        System.Windows.Forms.AccessibleEvents.DefaultActionChange;

      string result = myVal.ToString();
      // string result = ((object)myVal).ToString(); // Work around
      MessageBox.Show(result);
}
What do you think about this topic? Send feedback!