首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Obfuscar重命名内部类

用Obfuscar重命名内部类
EN

Stack Overflow用户
提问于 2015-01-19 19:09:21
回答 1查看 998关注 0票数 1

我需要在下面的Secret1中混淆(重命名)内部类。这是一个最小的WPF应用程序,带有辅助的publicinternal类,用于使用Obfuscar。

代码语言:javascript
复制
namespace WpfApp
{
    public enum Category { Low, High }

    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);
            var s1 = new Secret1();
            s1.SecretMethod1();
            var s2 = new Secret2();
            s2.SecretMethod();
        }
    }

    internal class Secret1
    {
        internal int SecretInt;
        private float SecretFloat;

        internal int SecretMethod1()
        {
            if (SecretMethod2() == false)
                return 0;
            else
                return 1;
        }

        private bool SecretMethod2()
        {
            return false;
        }
    }

    public class Secret2
    {
        private int SecretInt;

        public int SecretMethod()
        {
            return this.SecretInt;
        }
    }

    [Serializable] 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();
            }
        }
    }

    [Serializable] 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));
        }
    }
}

我的配置文件是:

代码语言:javascript
复制
<?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="false" />
      <Var name="HidePrivateApi" value="true" />
      <SkipType name="WpfApp.ViewModel" skipFields="true" skipProperties="true" />
      <SkipType name="WpfApp.WpfNotifier" skipFields="true" skipProperties="true" />
      <SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />
    </Module>

  </Obfuscator>

</configuration>

设置KeepPublicApiHidePrivateApi的所有可能组合都会导致跳过Secret1类。“跳过”很可能是“没有混淆”的术语。从我的笔记来看:

代码语言:javascript
复制
<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="false" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="true" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="false" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="true" />
Secret1 class was skipped

下面是Obfuscar映射输出的两个例子。

摘录自Mapping.txt (备公共=假,隐藏私有=真):

代码语言:javascript
复制
[WpfApp]WpfApp.Secret1 skipped:  HidePrivateApi option in configuration
{

    [WpfApp]WpfApp.Secret1::SecretMethod1[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::SecretMethod2[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::.ctor[0]( ) skipped:  special name


    System.Int32 [WpfApp]System.Int32 WpfApp.Secret1::SecretInt skipped:  HidePrivateApi option in configuration
    System.Single [WpfApp]System.Single WpfApp.Secret1::SecretFloat skipped:  HidePrivateApi option in configuration
}

摘录自Mapping.txt (公开为假,隐藏为私有=假)

代码语言:javascript
复制
[WpfApp]WpfApp.Secret1 skipped:  HidePrivateApi option in configuration
{

    [WpfApp]WpfApp.Secret1::SecretMethod1[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::SecretMethod2[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::.ctor[0]( ) skipped:  special name


    System.Int32 [WpfApp]System.Int32 WpfApp.Secret1::SecretInt skipped:  HidePrivateApi option in configuration
    System.Single [WpfApp]System.Single WpfApp.Secret1::SecretFloat skipped:  HidePrivateApi option in configuration
}

一个可接受的解决方案不应该要求修改源代码,因为实际应用程序比这个最小的WPF示例要大。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-25 12:22:38

我希望你能仔细检查一下样本项目,

https://github.com/lextm/obfuscar/blob/master/Examples/BasicExample/obfuscar.xml

<Var>标签必须放在<Obfuscator>标签下。否则,奥布斯卡尔就认不出他们了。

我必须承认,设置系统设计很差(由原来的开发人员),但我没有时间来取代它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28031859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档