如何反序列化一个类 [英] How Deserialize a class

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

问题描述

先生,

我有以下要求。请帮忙。

i have some requirement as follows. Please help.

class test01

class test01

{

public byte [] var1 = new byte [6] ;

public byte[] var1 = new byte[6];

public byte [] var2 = new byte [10];

public byte[] var2 = new byte[10];

}

var abc =(test01)反序列化(stream,typeof(test01));

var abc = (test01)Deserialize(stream, typeof(test01));

这是我的实际代码。当"流"时,上述情况将起作用。 object包含var1和var2,大小相同。

this is my actual code. the above case will work when the "stream" object contains var1 and var2 with same size.

我的问题:

我有var1和var2的流,但var2的大小是20而不是10。

i have stream with var1 and var2 but var2 size is 20 instead of 10.

我可以使用< test01类进行反序列化吗?

can i make use test01 class to Deserialize?

或者我需要创建一个如下所示的新类:

or i need to create an new class like below:

class test02

class test02

{

public byte [] var1 = new byte [6];

public byte[] var1 = new byte[6];

public byte [] var2 = new byte [20];

public byte[] var2 = new byte[20];

}

var abc =(test02)反序列化(stream,typeof(test02) ));

var abc = (test02)Deserialize(stream, typeof(test02));

请让我知道最佳方法?

如果类test01包含更多属性,那么去创建类test02是个好主意吗?

if the class test01 contains more attributes is it good idea to go to create class test02?

我想使用test01类。请帮助您找到最佳解决方案。

I want to make use the test01 class. Please help the best solution.

问候,

Ravi

推荐答案

您没有提供有关Deserialize方法的任何信息。在不知道您如何阅读数据的情况下,我们无法为您提供帮助。如果该方法只是使用BinaryReader来读取数据,那么最终会丢失
数据的后半部分。大多数二进制协议包括指定所接收数据的版本(因此其格式)的版本标头,或者对于灵活结构,包括指示要读取的值有多大的长度或计数或大小值。如果你没有
中的任何一个,那么你将无法处理这种情况。

You didn't provide any information about your Deserialize method. Without knowing how you read the data we aren't going to be able to help you. If that method simply uses a BinaryReader to read the data then you would end up missing the last half of your data. Most binary protocols include either a version header that specifies what version of the data being received (and hence its format) or, for flexible structures, a length or count or size value that indicates how big the value is to be read. If you don't have either of these then you aren't going to be able to handle this scenario.

我的个人建议是test02没有指定数组的大小。写出数据时,写出大小,然后写出数组内容。在阅读时,读入大小,分配数组,然后将数据读入
到数组中。 

My personal recommendation would be that test02 doesn't specify the size of the arrays at all. When writing out the data the size is written out and then the array contents. On reading the size is read in, the array is allocated and then the data is read into the array. 

如果你需要支持多种不同的结构,那么你'我需要在数据前面有一个标题块来告诉你你正在处理什么。标题将指定要读取的数据类型,然后您将实例化相应的实例,然后
从那里读取数据。但是,我不会对变量数组大小使用不同的类型,只有当实际结构不同时才会使用。

If you need to support multiple different structures then you'll need a header block in front of the data to tell you what you're dealing with. The header would specify the type of data to be read and then you'd instantiate the appropriate instance and then read the data from there. I wouldn't use different types for variable array sizes though, only if the actual structure is different. 


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

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