本文介绍了从MemoryStream反序列化会在C#中引发OutOfMemory异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#3.0 Winform问题。

This is a C# 3.0 Winform problem.

我想克隆一个大(实际上不是很大)的复杂对象。为了做到这一点,我将它序列化为MemoryStream并使用BinaryFormatter.UnsafeDeserialize(MemoryStream,null)方法来获取克隆的对象。在UnsafeDeserialize()方法内部,始终会引发OutOfMemory异常。内存应该足够。

I want to clone a big(not very big actually), complicated object. To do it lazily, I seralize it to a MemoryStream and use BinaryFormatter.UnsafeDeserialize(MemoryStream, null) method to get the cloned object. Inside the UnsafeDeserialize() method, there is always an OutOfMemory exception thrown. The memory should be sufficient.

我读了一些有关.net的错误,但不确定是否是这种情况。

I read a bit about this as a .net bug but not sure whether it is the case here.

任何人都知道为什么会这样吗?是否有工作流程?谢谢。

Anyone has any idea why this is happening? Any workround? Thanks.

推荐答案

也许是一个愚蠢的问题,但是您还记得在反序列化之前将MemoryStream倒回到流的开头吗?

Perhaps a silly question but are you remembering to rewind the MemoryStream to the start of the stream before deserializing it?

它也可能有助于共享您的一些代码。

It might also help to share some of your code.

这篇关于从MemoryStream反序列化会在C#中引发OutOfMemory异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:43