假设我有classA,其中包含classB,两者都是[Serializable].
我假设,在反序列化中,classB将首先被反序列化.
但事实并非如此,因为我可以通过记录每个[OnDeserialized]方法被击中来确认.
现在我有以下问题:
在classA的反序列化完成后,它应该使用classB中的值进行自我设置.不幸的是,此时尚未对classB进行反序列化,因此classA设置错误.
我的问题将得到解决,如果我可以强制BinaryFormatter在classA之前反序列化classB,或者将对象图从底部到顶部而不是从上到下解析.
另一个明显的解决方案是让classB在反序列化时激活一个事件然后让classA自行设置,但我想远离这个非优雅的解决方法.
如果有人知道更好的解决方案,我将不胜感激.
我想将一个数组模型对象序列化为二进制流.模型类主要具有字符串和整数属性.
我相信我可以将类标记为[Serializable]并使用二进制格式,但是我有兴趣知道您是否认为这是最好的方式,请记住我的优先级是尽可能减小文件的传输通过低带宽连接(我也可以压缩/解压缩文件).
该文件可能有1000条记录,所以理想情况下我希望能够附加到磁盘并按记录读取磁盘记录,而不必将整个文件同时存储在内存中.
所以我的优先事项是:文件小,内存使用效率高.
也许有预先编写的框架?使用XML和CSV文件似乎很容易!希望它也是一种自定义二进制格式.
谢谢
我有一个.NET 4 WCF服务,它向客户端发送一些由客户端反序列化的大对象(~115Mb).对象第一次进入时反序列化很好.但是,所有后续调用都会抛出一个OutOfMemoryException.我已经检查过以确保我所有人IDisposables都被包裹起来using.我查看了与此类似的其他问题,例如BinaryFormatter outofmemory异常反序列化
和来自MemoryStream的Deserialize在C#中抛出OutOfMemory异常
.我尝试了一些人们推荐的解决方案,包括使用Simon Hewitt的Optimized Serializer.但是,最后,他仍然依赖于BinaryFormatter反序列化对象.
我抓住了OutOfMemoryException并查看了堆栈跟踪(见下文).跟踪似乎源于StringBuilder类中内存利用率的问题.我读过其他文章,关于如何StringBuilder在需要更多空间时使用(长度*2)算法导致内存问题.
at System.Text.StringBuilder.ToString()
at System.IO.BinaryReader.ReadString()
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectString(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
Run Code Online (Sandbox Code Playgroud)
有没有办法以BinaryFormatter不同的方式工作而不使用StringBuilder或是否有更好的替代方案来BinaryFormatter更好地管理内存?
c# stringbuilder out-of-memory binaryformatter deserialization
我目前正在编写一个双向地图类,我在类的序列化/反序列化方面遇到了一些麻烦(问题在底部).
这是相关课程的部分内容.
/// <summary>
/// Represents a dictionary where both keys and values are unique, and the mapping between them is bidirectional.
/// </summary>
/// <typeparam name="TKey"> The type of the keys in the dictionary. </typeparam>
/// <typeparam name="TValue"> The type of the values in the dictionary. </typeparam>
[Serializable]
public class BidirectionalDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IEquatable<BidirectionalDictionary<TKey, TValue>>, ISerializable, IDeserializationCallback
{
/// <summary>
/// A dictionary that maps the keys to values.
/// </summary>
private readonly Dictionary<TKey, TValue> forwardMap;
/// …Run Code Online (Sandbox Code Playgroud) 我BinaryFormatter用来存储我的应用程序设置.现在,经过几年的持续开发,在许多用户已经在使用我的应用程序之后,我想要更改几个类的命名方式以及它们所在的命名空间.但是,如果我这样做,则无法再加载设置,因为BinaryFormater它们通过其代码内名称调用.
因此,例如,如果我更改MyNamespace.ClassOne为MyNamespace.Class.NumberOne代码,我将无法再加载设置,因为MyNamespace.ClassOne不再存在.
我想要进行此更改并允许用户保留其设置文件.这可能吗?
我的意思是,我想我可以研究它保存的格式,并手动更改二进制文件,替换类名,但这将是黑客的方法.必须有一个正常的方法,对吗?
我是新来的对象序列化,在我学习的过程中如何使用读取和写入文件(并行化和串行)BinaryFormatter,我遇到了BinaryReader和BinaryWriter,这似乎是做同样的事情.
有没有之间的一些微妙的差异BinaryFormatter.Serialize()和BinaryWriter?或者BinaryWriter只是执行相同操作的更紧凑的方式BinaryFormatter.Serialize()?
c# serialization binaryformatter binarywriter deserialization
我使用BinaryFormatter和a MemoryStream来序列化一个对象,然后将它作为二进制blob存储在数据库中.然后,我从数据库中检索数据,并使用binaryformatter和内存流进行反序列化.
但是,当我尝试反序列化对象时,我经常会抛出异常.最值得注意的'an object with the same key already exists'还是'cannot convert string to int64'
有没有人知道为什么反序列化掷骰子?或者如何找出哪些字典对象有麻烦?
我的序列化功能如下......
private byte[] SerializeUserData(UserData ud)
{
byte[] data = null;
using (MemoryStream ms = new MemoryStream())
{
ms.Seek(0, SeekOrigin.Begin);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, ud);
data = ms.ToArray();
}
return data;
}
private UserData Deserialize()
{
UserData ud = null;
using (MemoryStream ms = new MemoryStream(mSession.BinarySession))
{
BinaryFormatter bf = new BinaryFormatter();
ud = bf.Deserialize(ms) as UserData;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试将对象图序列化为字符串,然后从字符串反序列化它.如果我这样做,对象序列化就好了
using (var memStream = new System.IO.MemoryStream())
{
mf.Serialize(memStream, this);
memStream.Seek(0, 0);
Search s;
using (var memStrClone = new System.IO.MemoryStream())
{
memStream.CopyTo(memStrClone);
memStrClone.Seek(0, 0);
s = mf.Deserialize(memStrClone) as Search;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,但序列化为一个字符串,并尝试反序列化相同的字符串
Search s;
string xml = ToString<Search>(this);
s = FromString<Search>(xml);
public static TType FromString<TType>(string input)
{
var byteArray = Encoding.ASCII.GetBytes(input);
using (var stream = new MemoryStream(byteArray))
{
var bf = new BinaryFormatter();
return (TType)bf.Deserialize(stream);
}
}
public static string ToString<TType>(TType data)
{
using (var ms = new MemoryStream())
{ …Run Code Online (Sandbox Code Playgroud) 根据.NET CoreFx API列表及其相关的.NET Platform Standard版本,System.Runtime.Serialization.Formatters从1.3开始被添加到.NET平台标准中,这很酷,但是当我尝试创建.Net时核心类库目标netstandard1.5在.Net Core RC2下,我无法使用它.
代码很简单,只是想要声明一个BinaryFormatter:
public class Problems {
private System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _formatter;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
错误CS0234名称空间"System.Runtime"中不存在类型或命名空间名称"序列化"(您是否缺少程序集引用?)
这是project.json,我没有做任何修改:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027",
},
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50"
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我需要依赖的另一个包吗?为什么?对于列表中的所有API,网络标准名称不应该足够吗?
我已将几个 ASP.Net Core 2.2 项目迁移到 .Net 5,我遇到的最后一个问题是在尝试从项目资源加载位图时出现 System.NotSupported 异常。
RtfUtility.AppendLogo(result, Properties.Resources.Logo);
Run Code Online (Sandbox Code Playgroud)
System.NotSupportedException HResult=0x80131515 Message=BinaryFormatter 在此应用程序中禁用序列化和反序列化。有关详细信息,请参阅https://aka.ms/binaryformatter 。源=System.Runtime.Serialization.Formatters
我没有明确使用BinaryFormatter但奇怪的是,以相同的方式加载二进制 PDF 文件时我没有收到错误:
processor.LoadDocument(new MemoryStream(Properties.Resources.Certificate));
Run Code Online (Sandbox Code Playgroud)
两种机制都使用ResourceManager.GetObject,所以我不确定发生了什么。我知道我可以关闭项目文件中的错误,但这似乎是一个短期解决方案,我宁愿修复它并忘记它。感谢您可以提供的任何建议...
编辑:
堆栈跟踪如下,该错误不是由库引起的,而是在访问资源时引起的。当嵌入或链接图像时会发生这种情况 - 似乎没有什么区别,但对于(二进制)PDF 文件不会发生这种情况。
感谢您查看此...
在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(流序列化流)在System.Resources.ResourceReader。<>c__DisplayClass7_0`1.b__0(对象obj,流流)在System.Resources.ResourceReader.DeserializeObject(Int32 typeIndex) ) 在 System.Resources.ResourceReader._LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) 在 System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) 在 System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) 在 System.Resources .RuntimeResourceSet.GetObject(字符串键,布尔ignoreCase,布尔isString)在System.Resources.RuntimeResourceSet.GetObject(字符串键,布尔ignoreCase)在System.Resources.ResourceManager.GetObject(字符串名称,CultureInfo文化,布尔wrapUnmanagedMemStream)在System. C:\Development\Project\Project.Infrastruct\Properties\Resources.Designer.cs 中 Project.Infrastruct.Properties.Resources.get_Logo() 处的 Resources.ResourceManager.GetObject(String name, CultureInfo Culture):Project.Infrastruct 处的第 227 行C:\Development\Project\Project.Infrastruct\Models\ShowWording.cs 中的 .Models.ShowWording.Generate():第 146 行
binaryformatter ×10
c# ×10
.net ×4
asp.net ×1
binary ×1
binaryfiles ×1
binarywriter ×1
memorystream ×1