使Obfuscar避免重命名枚举类型

时间:2015-01-14 10:55:28

标签: c# enums obfuscar

在使用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
}

我的用法是错误还是错误?

1 个答案:

答案 0 :(得分:0)

已经发现了一个错误&#34; MarkedOnly&#34;选项,在混淆字段等时无法检查。我只是在主分支中修复它。

https://github.com/lextm/obfuscar

请注意,在此更改之后&#34; MarkedOnly&#34;选项仅适用于其他选项。如果元素(类/枚举/方法/字段等)没有附加Obfuscation属性,它将保持不变。设置如&#34; KeepPublicApi&#34;和&#34; HidePrivateApi&#34;被忽略了。