C# 序列化列表<;ICustomClass>;到xml?
我有以下代码文件:C# 序列化列表<;ICustomClass>;到xml?,c#,.net,xml-serialization,C#,.net,Xml Serialization,我有以下代码文件: 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; }
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; }
....
}
但我得到了这个错误:
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.
帮助?不能序列化接口,只能序列化具有默认XmlSerializer的具体类。您可以实现以覆盖此行为。由于接口的抽象性质,您无法序列化接口。许多具体类型可以实现相同的接口,因此会产生歧义。您必须使用具体类型。但我必须使用接口,否则整个程序将无法运行。@TheAdamGaskins接口只不过是一个带有纯虚拟方法的抽象基类。为什么“必须”是抽象基类上的接口?啊,误解了你所说的“必须使用具体类型”。但是将接口更改为抽象类会神奇地修复一切吗?@TheAdamGaskins这不是“魔法”,如果你使用公共(抽象)基类而不是接口,然后序列化程序将能够保留对象的正确完全限定类型信息。你不能用接口来做这件事。所以,它将解决这个问题。这似乎已经解决了它。谢谢:)
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.