XML 序列化和列表时间:2019-03-17 标签:c#
当我收到以下错误时,我正在尝试使用 XMLSerializer
序列化枚举的 List<>
:
'GameDataBuilder.vshost.exe' (Managed): Loaded 'uoqssn9i'
A first chance exception of type 'System.NullReferenceException' occurred in uoqssn9i
A first chance exception of type 'System.TypeInitializationException' occurred in uoqssn9i
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
我已查看了内部List<>
且所有值均有效。使用相同的代码,我可以序列化枚举(不是列表)和所有其他类型(int、float 等)以及这些其他类型的列表。看起来枚举列表会导致错误。
以前有人遇到过这个问题吗?
任何帮助将不胜感激
编辑:
序列化方法:
public void SerialiseToXML(XmlSerializer serializer, string directory)
{
string fileName = directory + m_Name + ".xml";
if (!Directory.Exists(directory))
{
DirectoryInfo di = Directory.CreateDirectory(directory);
}
if (!File.Exists(fileName))
{
using(File.Create(fileName))
{
}
}
using (TextWriter textWriter = new StreamWriter(fileName))
{
serializer.Serialize(textWriter, m_Objects);
}
}
序列化器:
private void GenerateSerializer()
{
List<Type> dynamiclyCreatedTypes = mTypeManager.GetSerializeableTypes();
m_Serializer = new XmlSerializer(typeof(List<ISerializeable>), dynamiclyCreatedTypes.ToArray());
}
列表是这样生成的:
type = typeof(List<>).MakeGenericType(type);
I am trying to serialise a List<>
of enums with the XMLSerializer
when I get the following Error:
'GameDataBuilder.vshost.exe' (Managed): Loaded 'uoqssn9i'
A first chance exception of type 'System.NullReferenceException' occurred in uoqssn9i
A first chance exception of type 'System.TypeInitializationException' occurred in uoqssn9i
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
I have taken a look inside the List<>
and all of the values are valid. Using the same code I can serialize an enum (not list) and all other types (int, float etc) and Lists of those other types just fine. It just seems like the list of enum causes errors.
Anyone came across this problem before?
Any help would be much appreciated
edit:
Serialize Method:
public void SerialiseToXML(XmlSerializer serializer, string directory)
{
string fileName = directory + m_Name + ".xml";
if (!Directory.Exists(directory))
{
DirectoryInfo di = Directory.CreateDirectory(directory);
}
if (!File.Exists(fileName))
{
using(File.Create(fileName))
{
}
}
using (TextWriter textWriter = new StreamWriter(fileName))
{
serializer.Serialize(textWriter, m_Objects);
}
}
Serializer:
private void GenerateSerializer()
{
List<Type> dynamiclyCreatedTypes = mTypeManager.GetSerializeableTypes();
m_Serializer = new XmlSerializer(typeof(List<ISerializeable>), dynamiclyCreatedTypes.ToArray());
}
The List Is Generated like this:
type = typeof(List<>).MakeGenericType(type);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您正在使用
typeof(List)
创建 XmlSerializer。序列化器不能使用接口,您需要为它们提供具体的类型。您可以使用这个
typeof(List)
来代替吗?I think the problem is that you are using
typeof(List<ISerializeable>)
to create the XmlSerializer. Serializers cannot work of interfaces, you need to provide them with a concrete type.Can you use this
typeof(List<EnumType>)
instead ?