enumsobfuscar

Obfuscar SkipType configuration element is not working for enums


The Obfuscar SkipType configuration element seems to be not working for enums. This is my fairly minimal configuration file.

<?xml version="1.0"?>
<configuration>

  <startup><supportedRuntime version="v4.0" 
     sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>

  <Obfuscator>

    <Var name="InPath"  
value="\users\user\docs\vs2013\projects\wpfapp\wpfapp\bin\debug" />
    <Var name="OutPath" 
value="\users\user\docs\vs2013\projects\wpfapp\wpfapp\bin\debug" />

    <Module file="$(InPath)\wpfapp.exe" />

    <Var name="KeepPublicApi" value="true" />
    <Var name="HidePrivateApi" value="true" />

    <SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />

  </Obfuscator>

</configuration>

The map output file shows that the skipping did not work and the enum type Category was renamed.

Renamed Types:

[WpfApp]WpfApp.Category -> [WpfApp]A.a
{
    WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::Low -> A
    WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::High -> a

    System.Int32 [WpfApp]System.Int32 WpfApp.Category::value__ skipped:  special name
}

Edit: The element <SkipType name="WpfApp.Category" /> causes the same problem.

Edit: The element <SkipType name="WpfApp.Category" skipFields="true" /> causes the same problem.

Edit: The element <SkipField type="WpfApp.Category" name="*" /> causes the same problem.

Edit: This pair

<SkipField type="WpfApp.Category" name="Low" />

<SkipField type="WpfApp.Category" name="High" /> causes the same problem.

The source:

namespace WpfApp
{
    public enum Category { Low, High }

    //[System.Reflection.Obfuscation]
    public partial class MainWindow : Window
    {
        private ViewModel ViewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this.ViewModel = new ViewModel();
        }

        private void MyButtonClick(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Process(MyTextBox.Text);
        }
    }

    internal class ViewModel : WpfNotifier
    {
        private const float DefaultKilograms = 80.0f;

        private string _kilograms;
        public string Kilograms // WPF binds here
        {
            get { return this._kilograms; }
            set { this._kilograms = value; NotifyPropertyChanged(); }
        }
        private string _resultText;
        public string ResultText // WPF binds here
        {
            get { return this._resultText; }
            set { this._resultText = value; NotifyPropertyChanged(); }
        }

        internal void Process(string input)
        {
            float kilograms;
            if (Single.TryParse(input, out kilograms))
            {
                Category c = (kilograms > 100.0f) ? Category.High : Category.Low;
                this.ResultText = c.ToString();
            }
            else
            {
                this.Kilograms = ViewModel.DefaultKilograms.ToString();
            }
        }
    }

    public class WpfNotifier : INotifyPropertyChanged
    {
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged; // public for interface

        internal void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            else
                ; // it is harmless to fail to notify before the window has been loaded and rendered
        }
    }
}

Is this a bug or is my usage wrong?


Solution

  • Your usage is wrong. If you check the documentation you will see that <SkipType> tags must be put into <Module> tags. Otherwise, Obfuscar has no idea in which module/assembly this skip rule takes effect. So you should try

    <Module file="$(InPath)\wpfapp.exe">
        <SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />
    </Module>