XML 序列化和列表时间:2019-03-17 标签:c#

发布于 2024-11-03 14:49:12 字数 1615 浏览 1 评论 0原文

当我收到以下错误时,我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

绮筵 2024-11-10 14:49:12

我认为问题在于您正在使用 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 ?

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