【问题标题】:serialize obfuscated class C#序列化混淆类 C#
【发布时间】:2012-03-20 17:00:05
【问题描述】:

我目前有一个帮助类,我用它来混淆一个静态类,该类跟踪我正在开发的游戏中的高分。我在发布时使用 Eazfuscator,发现当我的分数被序列化时,抛出了这个异常:

ArgumentException 标识符“”不符合 CLS

有没有一种方法可以将我的高分列表存储在我的助手类中,并且在混淆后仍然能够对其进行序列化?

try
{
  GameHighScore highScoreHelper = new GameHighScore();
  highScoreHelper.CreateGameHighScore(highScore);

  XmlSerializer serializer = new XmlSerializer(typeof(GameHighScore));
  serializer.Serialize(stream, highScoreHelper);
}
catch(Exception e)
{
  Logger.LogError("Score.Save", e);
}

我的助手类:

  public class GameHighScore
  {
    
    public List<HighScoreStruct<string, int>> highScoreList;

    private HighScoreStruct<string, int> scoreListHelper;

    [XmlType(TypeName = "HighScore")]
    public struct HighScoreStruct<K, V>
    {
      public K Initials
      { get; set; }

      public V Score
      { get; set; }

      public HighScoreStruct(K initials, V score) : this() 
      {
        Initials = initials;
        Score = score;
      }
    }

    public GameHighScore()
    {
      highScoreList = new List<HighScoreStruct<string, int>>();
      scoreListHelper = new HighScoreStruct<string, int>();
    }

    public void CreateGameHighScore(List<KeyValuePair<string, int>> scoreList)
    {

      for (int i = 0; i < scoreList.Count; i++)
      {
        scoreListHelper = new HighScoreStruct<string, int>(scoreList[i].Key, scoreList[i].Value);
        highScoreList.Add(scoreListHelper);
      }
    }
  }

【问题讨论】:

    标签: c# xml-serialization eazfuscator


    【解决方案1】:

    最好的解决方案是不要混淆任何类型的序列化所需的类。这样做您将获得 2 个好处:

    • 类不会使用奇怪的名称
    • 重新运行混淆不会为相同的类/字段生成新名称。

    大多数混淆器允许指定保持特定类/方法不混淆的属性。

    否则 - 编写您自己的序列化。

    【讨论】:

    • [Obfuscation(Exclude = True)] 添加到我的结构中就像一个魅力,现在分数正确地序列化到一个xml文件中。谢谢!
    【解决方案2】:

    尝试在您的属性上使用XmlElement 属性。

    【讨论】:

    • +1。这可能会起作用并解决名称更改的问题。请注意,需要注释每个属性。
    • @AlexeiLevenkov 是的。好吧,我仍然不知道为什么需要混淆公共课程,但这是另一个问题。
    • 有没有办法标记我列表中的结构,以便可以通过代码混淆正确序列化?
    • @NexAddo 嗯,好问题,我不知道这样的事情。但是你为什么不像@Alexei 建议的那样在你的结构上使用[Obfuscation(Exclude = true)] 属性呢?
    • @BalazsTihanyi 我想我只是对他之前所说的话一无所知。既然你把它拼出来了,我记得 System.Reflection 有这样的标签。非常感谢。效果很好。
    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2011-12-02
    • 2012-01-08
    • 1970-01-01
    • 2011-12-22
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多