二进制序列化 [英] Binary serialization

查看:85
本文介绍了二进制序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是一个dotNet newby,所以原谅我的无知。


我正在寻找一种保存/复制托管类的方法流/文件

没有保存对象的状态,例如。如果我有一个带有两个int32'的

作为其数据成员的ref类,那个类的二进制文件必须大小为8

bytes(即仅包含类数据成员,而不是方法等。)


序列化是上述问题的答案吗?如果我理解正确,

指针算术和像sizeof(MyRefClass)

这样的操作不起作用的原因是托管类是由CLR处理的,反过来,

要求在移植之前对对象进行序列化。


简而言之,是否有一种序列化方法可以让我存储参考

类数据成员只有与原生C ++(memcpy等)相同的方式吗?


谢谢!

雅克

推荐答案

Bonjour Jacques,


听起来你需要的是一个实现ISerializable的类

界面和自定义序列化。以下文章和参考资料

应该有所帮助:


System.Runtime.Serialization命名空间:
http://msdn2.microsoft.com/en-us/lib...alization。 aspx


自定义序列化:
http://msdn2.microsoft.com/en-us/library/ty01x675.aspx

ISerializable.GetObjectData:
http:/ /msdn2.microsoft.com/en-us/lib...bjectdata.aspx


-

HTH,


Kevin Spencer

微软MVP

专业鸡肉沙拉炼金术士


大厚片由很多小的。

" Jacques" < JA ***** @ discussions.microsoft.com>在留言中写道

新闻:B8 ********************************** @ microsof t.com ...
Bonjour Jacques,

It sounds like what you need is a class that implements the ISerializable
interface, and custom Serialization. The following articles and references
should help:

The System.Runtime.Serialization Namespace:
http://msdn2.microsoft.com/en-us/lib...alization.aspx

Custom Serialization:
http://msdn2.microsoft.com/en-us/library/ty01x675.aspx

ISerializable.GetObjectData:
http://msdn2.microsoft.com/en-us/lib...bjectdata.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"Jacques" <Ja*****@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...

我是一个dotNet newby,所以原谅我的无知。

我正在寻找一种保存/复制托管的方法类到
流/文件
没有保存对象的状态,例如。如果我有一个带有两个
int32'的ref class作为其数据成员,那个类的二进制文件必须有8个字节的大小(即只包含类数据成员,不是方法等。)

序列化是上述问题的答案吗?如果我理解正确,那么指针算法和sizeof(MyRefClass)之类的操作不起作用的原因是托管类是由CLR处理的,在
转,
要求在移植之前对对象进行序列化。

简而言之,是否有一种序列化方法,使我能够以相同的方式存储ref />类数据成员就像在原生C ++(memcpy等)中那样?

谢谢!
Jacques
Hi
I am an dotNet newby, so pardon my ignorance.

I am looking for a method of saving/copying a managed class to a
stream/file
WITHOUT saving the object''s state, eg. if I have a ref class with two
int32''s
as its data members, the binary file of that class must have a size of 8
bytes (i.e. only contains class data members, not methods etc.).

Is serialization the answer to the above problem? If I understand
correctly,
the reason that pointer arithmetic and operations like sizeof(MyRefClass)
don''t work is that the managed class is handled by the CLR, which, in
turn,
requires objects to be serialized before porting.

In short, is there a serialization method which enables me to store ref
class data members ONLY in the same way as in native C++ (memcpy,etc.)
does?

Thanks!
Jacques



雅克写道:
Jacques wrote:

我是一个dotNet newby,所以原谅我的无知。

我正在寻找一种保存/复制方法托管类到
流/文件而不保存对象的状态,例如。如果我有一个带有两个int32'作为其数据成员的ref
类,那个
类的二进制文件必须有8个字节的大小(即只包含类数据
成员,不是方法等。)

序列化是上述问题的答案吗?
Hi
I am an dotNet newby, so pardon my ignorance.

I am looking for a method of saving/copying a managed class to a
stream/file WITHOUT saving the object''s state, eg. if I have a ref
class with two int32''s as its data members, the binary file of that
class must have a size of 8 bytes (i.e. only contains class data
members, not methods etc.).

Is serialization the answer to the above problem?




No.


二进制序列化是一种将_that_数据写入

输出流的机制,这是在读取精确数据时重新创建对象所必需的。 I.o.w。:你得到的内容+额外的绒毛。如果你想要保存8个字节,那么你应该抓住8个字节并自己写下它们b / b
。虽然我没有看到8字节文件的必要性。因此,
导致您想要将许多

对象的数据保存到一个文件中。如果你自己这样做,你可以得到一个

压缩文件,但也有很多代码,或者更好:代码多于

你最终会得到什么时候你会去二元格式化器

方法。


FB


-

---------------------------------------------- --------------------------

LLBLGen Pro的首席开发人员,.NET的高效O / R映射器

LLBLGen Pro网站: http://www.llblgen.com

我的.NET博客: http://weblogs.asp.net / fbouma

Microsoft MVP(C#)

----------------------- -------------------------------------------------



No.

Binary serialization is a mechanism which writes _that_ data to the
output stream which is necessary to re-create the object when reading
that precise data. I.o.w.: you get the contents + extra fluff. If you
want to save 8 bytes, you should grab the 8 bytes and write them
yourself. Though I fail to see the necessity for files of 8 bytes. This
thus leads to the assumption that you want to save the data of a lot of
objects into one file. If you do that yourself, you can end up with a
compact file, but also with a lot of code, or better: more code than
you would end up with when you would just go for the binary formatter
approach.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------





假设我不想保存8字节的文件你是对的!我写了一个实时数据捕获应用程序并需要最小化磁盘空间。

以前我会使用一个与memcpy类似的文件写例程,

即File.Write(& MyClass,sizeof(MyClass))。


我的问题是:我只能提取的唯一方法是数据

有问题的班级成员编写每个单独数据的方法

成员到astream / file .. MYSELF?我遇到的问题有两个:

1.每个自定义对象都需要自己的自定义编写器例程

2. point nr(1)非常容易出错而导致笨拙程序员喜欢我自己!


亲切的问候

雅克


" Frans Bouma [C#MVP]"写产品:>雅克写道:
Hi

You are correct in assuming that I don''t want do save files of 8 bytes! I am
writing a realtime data capture app and need to minimise disk space.
Previously I would use a file write routine that worked similarly to memcpy,
ie "File.Write(&MyClass,sizeof(MyClass))".

My question is this: Is the only way in which I can extract ONLY the DATA
members from the class in question the method of writing each individual data
member to astream/file.. MYSELF? The problem I have with this is twofold:
1. every custom object needs its own custom streamwriter routine
2. point nr (1) is quite error prone for a clumsy programmer like myself!

Kind regards
Jacques

"Frans Bouma [C# MVP]" wrote:> Jacques wrote:

我是一个dotNet newby,所以请原谅我的无知。

我在寻找用于将托管类保存/复制到流/文件而不保存对象的状态的方法,例如。如果我有一个带有两个int32'作为其数据成员的ref
类,那个
类的二进制文件必须有8个字节的大小(即只包含类数据
成员,不是方法等。)

序列化是上述问题的答案吗?
Hi
I am an dotNet newby, so pardon my ignorance.

I am looking for a method of saving/copying a managed class to a
stream/file WITHOUT saving the object''s state, eg. if I have a ref
class with two int32''s as its data members, the binary file of that
class must have a size of 8 bytes (i.e. only contains class data
members, not methods etc.).

Is serialization the answer to the above problem?





二进制序列化是一种机制它将_that_数据写入
输出流,这是在读取精确数据时重新创建对象所必需的。 I.o.w。:你得到的内容+额外的绒毛。如果你想要保存8个字节,你应该抓住8个字节并自己编写它们。虽然我没有看到8字节文件的必要性。因此,可以假设您希望将许多对象的数据保存到一个文件中。如果你自己这样做,你可以得到一个紧凑的文件,但也有很多代码,或者更好:代码比你想要的二进制代码更多格式化师
方法。

FB

-
-------------------- -------------------------------------------------- -
LLBLGen Pro的首席开发人员,这是.NET的高效O / R映射器LLBLGen Pro网站: http://www.llblgen.com
我的.NET博客: http://weblogs.asp.net/fbouma
Microsoft MVP(C#)
----------------- -------------------------------------------------- -----



No.

Binary serialization is a mechanism which writes _that_ data to the
output stream which is necessary to re-create the object when reading
that precise data. I.o.w.: you get the contents + extra fluff. If you
want to save 8 bytes, you should grab the 8 bytes and write them
yourself. Though I fail to see the necessity for files of 8 bytes. This
thus leads to the assumption that you want to save the data of a lot of
objects into one file. If you do that yourself, you can end up with a
compact file, but also with a lot of code, or better: more code than
you would end up with when you would just go for the binary formatter
approach.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------



这篇关于二进制序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆