让 Obfuscar 避免重命名枚举类型

make Obfuscar avoid renaming enum types

在使用 Obfuscar 时,我想防止枚举类型被混淆,因为我需要原始枚举值名称。我对枚举值调用 ToString() 因为它们对用户有用。我在使用常规配置时遇到了困难,其中所有类型都被混淆了,除了那些出现在配置文件中带有 <SkipType name="namespace.EnumType"/> 元素的类型。我正在诉诸使用 <MarkedOnly /> 的更悲观的方法,它只混淆标有注释的内容。下面是相当小的配置文件。

    <?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\documents\visual studio 2013\projects\wpfapp\wpfapp\bin\debug" />
        <Var name="OutPath" 
value="\users\user\documents\visual studio 2013\projects\wpfapp\wpfapp\bin\debug" />

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

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

        <Var name="MarkedOnly" value="true" />

      </Obfuscator>

    </configuration>

注释代码为:

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
        }
    }
}

如您所见,只有一种类型用 [System.Reflection.Obfuscation] 注释,但输出映射显示枚举已重命名。枚举类型称为 Category.

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
}

是我的用法有误还是这是一个错误?

在 "MarkedOnly" 选项周围发现了一个错误,在混淆字段等时无法检查它。我刚刚在 master 分支中修复了它。

https://github.com/lextm/obfuscar

请注意,此更改后 "MarkedOnly" 选项是其他选项所独有的。如果一个元素(class/enum/method/field 等)没有附加 Obfuscation 属性,它将保持不变。 "KeepPublicApi" 和 "HidePrivateApi" 等设置将被忽略。