在使用 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
}
我的用法是错误的还是这是一个错误?