简体   繁体   English

使用BinaryFormatter序列化复杂对象

[英]Serializing a complex object with BinaryFormatter

I am trying to serialize a complex object which contains 2 lists of complex objects using the code below 我正在尝试使用下面的代码序列化包含2个复杂对象列表的复杂对象

public static byte[] SerializeObject(object obj)
{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream();
    formatter.Serialize(stream, obj);
    return stream.ToArray();
}

When I deserialize though I get NHibernate exceptions that my list objects failed to initialize, so I suspect that they haven't been serialized correctly in the first place. 当我反序列化但遇到NHibernate异常时,我的列表对象无法初始化,因此我怀疑它们最初并没有正确地序列化。 The error I receive is failure to lazily initialize a collection of some object, no session or session was closed. 我收到的错误是无法延迟初始化某些对象的集合,没有会话或会话被关闭。

But if they were properly serialized then there would not be a need to lazily initialize, they would already be there, right? 但是,如果对它们进行了适当的序列化,那么就不需要延迟初始化,它们已经在那里了,对吗?

What may be happening here is that you are serializing NHibernate proxies for the collections. 这里可能发生的事情是您正在序列化NHibernate代理集合。 Depending on your mapping, for performance reasons, NHibernate will not load a collection up until the point you explicitly access its elements. 出于性能原因,取决于您的映射,NHibernate直到您显式访问其元素时才会加载集合。
It's also able to do this for associations of various kinds (it's called 'lazy loading') and the way it works is that NHibernate actually instantiates and uses a proxy object that implements the correct interface (or derives from your classes in case of other associations). 它还可以对各种类型的关联(称为“延迟加载”)执行此操作,并且其工作方式是NHibernate实际上实例化并使用实现正确接口的代理对象(或在其他关联的情况下从您的类派生) )。

You may already know all that, but I'm explaining it for context in case you don't. 您可能已经知道所有这些,但是如果您不了解,我将在上下文中进行解释。 If you need to know more about lazy loading check out this article: http://nhibernate.info/doc/howto/various/lazy-loading-eager-loading.html 如果您需要了解更多有关延迟加载的信息,请查看以下文章: http : //nhibernate.info/doc/howto/various/lazy-loading-eager-loading.html

In this case, NHibernate may be using a proxy for your lists and as the BinaryFormatter is accessing them in a non-conventional way that is what you end up serializing. 在这种情况下,NHibernate可能会为您的列表使用代理,并且由于BinaryFormatter以非常规的方式访问它们(最终导致序列化)。

If this is the case, there are many ways in which you could go ahead and fix it and they depend on how you are structuring your project. 如果是这样,您可以采用多种方法进行修复并取决于他们如何构造项目。

A quick way to confirm if this takes care of your issues is, before serializing your object go ahead and initialize its lazy properties (note that you need to do this for each one or recursively as the Initialize method will only load data for the proxy you give to it): 确定序列是否能解决问题的一种快速方法是,在序列化对象之前,先初始化其惰性属性(请注意,您需要为每个对象或递归地执行此操作,因为Initialize方法只会为您加载的代理加载数据给它):

NHibernateUtil.Initialize(yourObject);
NHibernateUtil.Initialize(yourObject.List1);
NHibernateUtil.Initialize(yourObject.OtherList);
...etc...
SerializeObject(yourObject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM