【问题标题】:XmlInclude for derived class with no acess to base class?XmlInclude 用于无法访问基类的派生类?
【发布时间】:2017-02-22 02:55:02
【问题描述】:

我有一个派生类,它只向基类添加方法,我想将它序列化为其基类,我遇到的问题是这是一个游戏模组,我无法修改基类直接将 XmlInclude 添加到基础。

namespace CustomNPC
{
public class CustomNPCInteract : NPC
{
public CustomNPCInteract()
  :base()
{
  this.initializeNPC();
}

public CustomNPCInteract(AnimatedSprite sprite, Vector2 position, string defaultMap, int facingDir, string name, bool dateable, Dictionary<int, int[]> schedule, Texture2D portrait)
  : base(sprite, position, defaultMap, facingDir, name, dateable, schedule, portrait)
{
  this.initializeNPC();
}

public CustomNPCInteract(AnimatedSprite sprite, Vector2 position, int facingDir, string name, StardewValley.LocalizedContentManager manager = null)
  :base(sprite, position, facingDir, name, manager)
{
  this.initializeNPC();
}

private void initializeNPC()
{
  this.age = 2; //child
  this.manners = 2; //rude
  this.socialAnxiety = 1; //shy
  this.optimism = 1; //negative
  this.gender = 1; //female
  this.datable = false; //not-datable
  this.homeRegion = 0; //Other
  this.birthday_Season = "fall"; //fall 8
  this.birthday_Day = 8;
    }
}
}

我的命名空间和类是如何设置的。 CustomNPCInteract 继承 NPC,我希望它像任何其他 NPC 一样序列化,但无法修改该特定类。

任何帮助将不胜感激

【问题讨论】:

    标签: c# .net xml serialization


    【解决方案1】:

    这就是你的做法......

            XmlSerializer serializer = new XmlSerializer(typeof(CustomNPCInteract),new XmlRootAttribute("NPC"));
    

    这会将序列化 XML 的根重命名为 NPC,以便您可以使用标准反序列化

    【讨论】:

    • 感谢您的评论。我正在为其编写 mod 的实际游戏在 SaveGame.serializer 下已经有自己的序列化程序实例,它用于保存数据。是否可以附加到它而不是覆盖它们现有的实例?
    • 没有。您将不得不覆盖它。 XML 不适合附加数据,因为任何 XML 文档中最多有一个根节点。用您自己的序列化程序替换默认序列化程序应该是一件简单的事情,但在不了解更多信息的情况下,我无法提出任何进一步的建议。
    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2012-04-28
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多