派生类的XmlInclude没有访问基类?

时间:2017-02-22 02:55:03

标签: c# .net xml serialization

我有一个派生类,它只向基类添加方法,我想将它序列化为它的基类,我遇到的问题是这是一个游戏模式,我无法修改基类直接将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一样序列化,但没有访问权限来修改那个特定的类。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

你就是这样做的......

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

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

相关问题