本文介绍了Base64的反序列化期间空引用异常(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的方法来序列化和反序列化.NET对象:

I am using the following methods to serialize and deserialize .NET objects:

public static string SerializeToBase64(object data)
{
    var stream = new MemoryStream();
    var formatter = new BinaryFormatter();
    formatter.Serialize(stream, data);
    stream.Position = 0;
    return Convert.ToBase64String(stream.ToArray());
}

public static object DeserializeFromBase64(string data)
{
    var stream = new MemoryStream(Convert.FromBase64String(data));
    stream.Position = 0;
    var formatter = new BinaryFormatter();
    return formatter.Deserialize(stream);
}

这些方法似乎与标有[Serializable]属性类的简单工作时正常工作。

These methods seem to work fine when working with simple classes marked with the [Serializable] attribute.

但我需要用这个code序列化由一个ORM框架,即每个实体类来说,从我没有源$ C ​​$ C基类派生创建实体类(也maked为Serializable)

But I need to use this code to serialize entity classes (also maked as Serializable) created by an ORM framework, whereby each entity class is derived from a base class for which I do not have source code.

当与实体类的实例工作,它完成系列化没有异常,但在反序列化总是抛出excecuting formatter.Deserialize()。当一个空引用异常

When working with instances of an entity class, it completes serialization without exceptions, but deserialization always throws a null reference exception when excecuting formatter.Deserialize().

我不是很熟悉,序列化的过程,但我认为这个问题必须由一些目标对象的状态异常引起的。是否有一个标准的一套标准,一个对象必须串行化之前见面吗?

I am not very familiar with the process of serialization, but I assume this problem must be caused by something anomalous in the state of the target object. Is there a standard set of criteria that an object must meet before serialization?

其他任何调试建议将​​是AP preciated。

Any other debugging suggestions would be appreciated.

谢谢,
蒂姆·

Thanks,Tim

更新:

在进一步的实验,我想我已经发现了问题的原因。目标对象具有由未标记为可序列另一个类处理,如的。

After further experimentation, I think I have discovered the cause of the problem. The target object has events that are handled by another class that is not marked as serializable, as described in this post.

有趣的是,serialaztion正常工作,甚至与附加事件处理程序 - 这是反序列化失败

What's interesting is that serialaztion works correctly, even with the event handlers attached - it's deserialization that fails.

但我暂时移除事件处理程序和序列化和反序列化工作正常,所以我认为这是问题的测试。然而,由于我没有访问code,其中的事件声明,我不能马上看到如何实现上述解决方案。这可能是因为我不得不修改我的序列化过程中删除,然后恢复事件处理程序。

But I have tested by temporarily removing the event handlers and both serialization and deserialization works correctly, so I assume this is the problem. However, since I don't have access to the code in which the events are declared, I can't immediately see how to implement the solution described above. It may be that I have to modify my serialization process to remove and then reinstate the event handlers.

推荐答案

哪些ORM框架是什么呢?需要注意的是ORM生成的类型往往是的尤其的厌恶与的BinaryFormatter 使用时,因为他们并不总是POCO:他们往往有场这涉及到ORM - 所以他们创造了独立的问题。总之,我不是的非常的惊讶,它没有在这种情况下工作。

Which ORM framework is it? Note that ORM-generated types tend to be particularly obnoxious when used with BinaryFormatter, since they aren't always "POCO": they often have fields that relate to the ORM - so creating them standalone has issues. In short, I'm not hugely surprised that it doesn't work in this case.

您可能要考虑使用类似的DataContractSerializer 的XmlSerializer ,protobuf网,或者 NetDataContractSerializer - 这些都做了类似的工作,而是因为他们对公共属性的作用(而不是场),他们的往往的更有效 - 甚至还有很多有内置的对于这些方法用作DTO的支持。

You might want to consider using something like DataContractSerializer, XmlSerializer, protobuf-net, or maybe NetDataContractSerializer - these all do a similar job, but because they work on public properties (rather than fields) they tend to be more effective - and indeed many have inbuilt support for these approaches for use as a DTO.

这篇关于Base64的反序列化期间空引用异常(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:23