序列化列表到 XML?

发布于 2024-12-13 13:24:13 字数 1729 浏览 2 评论 0原文

我有以下代码文件:

public interface IMod
{
    string Name { get; set; }
    string Description { get; set; }
    bool Enabled { get; set; }
    List<IClassFile> ClassFiles { get; set; }
}
public interface IClassFile
{
    string Path { get; set; }
    string FileName { get; set; }
    bool Enabled { get; set; }
}
public class ClassFile : IClassFile
{
    public string Path { get; set; }
    public string FileName { get { return System.IO.Path.GetFileName(Path); } }
    public bool Enabled { get; set; }

    ....
}
public class ZippedMod : IMod
{
    public string Name { get; set; }
    public string Description { get; set; }
    public bool Enabled { get; set; }
    public List<IClassFile> ClassFiles { get; set; }

    ....
}
public class ConfigurationBlock
{
    public List<IMod> Mods { get; set; }

    ....
}

在程序的整个过程中,我向 ConfigurationBlock 添加了一些 ZippedMod,但现在我想序列化它们。我尝试这样做:

using (var stream = new StreamWriter("config.xml"))
{
    var ser = new XmlSerializer(typeof(ConfigurationBlock));
    ser.Serialize(stream, configBlock);
}

但我收到此错误:

There was an error reflecting type 'MinecraftModManager.ConfigurationBlock'.
|-Inner Exception:
      Cannot serialize member 'MinecraftModManager.ConfigurationBlock.Mods' of type 'System.Collections.Generic.List`1[[MinecraftModManager.IMod, MinecraftModManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.
      |-Inner Exception:
            Cannot serialize member MinecraftModManager.ConfigurationBlock.Mods of type MinecraftModManager.IMod because it is an interface.

帮助?

I have the following code files:

public interface IMod
{
    string Name { get; set; }
    string Description { get; set; }
    bool Enabled { get; set; }
    List<IClassFile> ClassFiles { get; set; }
}
public interface IClassFile
{
    string Path { get; set; }
    string FileName { get; set; }
    bool Enabled { get; set; }
}
public class ClassFile : IClassFile
{
    public string Path { get; set; }
    public string FileName { get { return System.IO.Path.GetFileName(Path); } }
    public bool Enabled { get; set; }

    ....
}
public class ZippedMod : IMod
{
    public string Name { get; set; }
    public string Description { get; set; }
    public bool Enabled { get; set; }
    public List<IClassFile> ClassFiles { get; set; }

    ....
}
public class ConfigurationBlock
{
    public List<IMod> Mods { get; set; }

    ....
}

Throughout the course of my program, I add a few ZippedMods to the ConfigurationBlock, but now I want to serialize them. I tried doing:

using (var stream = new StreamWriter("config.xml"))
{
    var ser = new XmlSerializer(typeof(ConfigurationBlock));
    ser.Serialize(stream, configBlock);
}

But I get this error:

There was an error reflecting type 'MinecraftModManager.ConfigurationBlock'.
|-Inner Exception:
      Cannot serialize member 'MinecraftModManager.ConfigurationBlock.Mods' of type 'System.Collections.Generic.List`1[[MinecraftModManager.IMod, MinecraftModManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.
      |-Inner Exception:
            Cannot serialize member MinecraftModManager.ConfigurationBlock.Mods of type MinecraftModManager.IMod because it is an interface.

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-12-20 13:24:13

由于接口的抽象性质,您无法序列化接口。许多具体类型可以实现相同的接口,因此会产生歧义。您必须使用具体类型。

You can't serialize an interface due to the abstract nature of them. Lots of concrete types can implement the same interface, so it creates an ambiguity. You must use a concrete type.

缪败 2024-12-20 13:24:13

您无法序列化接口,只能序列化具有默认 XmlSerializer 的具体类。您可以实施 IXmlSerialized 来覆盖这种行为。

You can't serialize an interface, only a concrete class with the default XmlSerializer. You may be able to implement IXmlSerializable to override this behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文