0

In our application, we have an extension for Enum

namespace System
{
    /// <summary>
    /// Contains extention methods for emuns.
    /// </summary>
    public static class EnumExtention
    {
        /// <summary>
        /// Check is value has flag.
        /// </summary>
        /// <param name="value">Checked value</param>
        /// <param name="checkedFlag">Checked flag.</param>
        /// <returns>True if enum contains specified flag. Otherwise false</returns>
        public static bool HasFlag(this Enum value, Enum checkedFlag)
        {
            ulong num = Convert.ToUInt64(checkedFlag);
            ulong num2 = Convert.ToUInt64(value);
            return (num2 & num) == num;
        }
    }
}

And somewhere in the project we have some Enum and some Func

[Flags]
public enum MyEnum
{
   zero= 0,
   two = 2,
   three = 3,
   threetwo = two | three 
}

public void SomeFunc()
{
   var ThreeTwo = Myenum.threetwo;

   bool _true = ThreeTwo.HasFlag(Myenum.three);
   bool _false = ThreeTwo.HasFlag(Myenum.zero);
   System.Windows.MessageBox.Show(String.Format("_true is: {0} and _false is : {1}", _true, _false);
 }

public void СallerFunction()
{
   try
   {
      SomeFunc();
   }
   catch (Exception ex)
   {
      System.Windows.MessageBox.Show(String.Format("Oops! {0}"), ex.Message));
   }
}

After build in release mode all wocks fine and we get this:

"_true is: true and _false is : false"

But aftet Dotfuscator worked on the code, we obtain the following:

"Oops! MethodNotFoundException"

And all Features set to Yes

Version of Dotfuscator: PreEmptive ver4.9.7000 WindowsPhoneEdition

What is wrong with the extension?

P.S. sorry for my english.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • What's the `StackTrace` property for the exception? - I know it will be obfuscated, but it may hint which method it thinks is missing. – DaveShaw Jul 06 '12 at 11:42

1 Answers1

1

Disclaimer: My experience is with the desktop version of dotfuscator, not the phone version.

Dotfuscator may be deciding to replace the enum definitions with the equivalent constant values everywhere the enum is referenced, thereby eliminating the need for the enum type.

Try using the Dotfuscator settings to exempt the enum from obfuscation, or using the following attributes on the enum:

[Obfuscation(Exclude=true, ApplyToMembers=true, StripAfterObfuscation=true, Feature="renaming"]
[Obfuscation(Exclude=false, ApplyToMembers=true, StripAfterObfuscation=true, Feature="conditionalinclude")]

...and then if that works try allowing renaming but not removal by using just the second attribute.

After obfuscation check with ILDASM to see if the enum type is still present and that dotfuscator doing what is expected.

Stephen Hewlett
  • 2,415
  • 1
  • 18
  • 31
  • Yes, it fixes the problem. Even if you only use the second attribute. Thank you. – Абросимов Станислав Jul 07 '12 at 10:43
  • P.S. Maybe you know a couple of useful articles about the obfuscation attributes. Please send a link to e-mail (stasabrosimov@gmail.com) or leave this in the comments. – Абросимов Станислав Jul 07 '12 at 10:55
  • 2
    :) Glad it worked. The attributes are part of the .NET framework as they are designed to be used with any obfuscator. See http://msdn.microsoft.com/en-us/library/system.reflection.obfuscationattribute.aspx]. Each tool is free to interpret them how it wishes. The difficult bit is the Exclude attribute, as dotfuscator will only obey the attribute if this is set correctly. It has to be set differently depending on which feature string is being set. I have a list somewhere of what dotfuscator requires, I'll try to find it. – Stephen Hewlett Jul 07 '12 at 13:52