基本类构造函数和序列化 [英] Basic class constructor and serialization

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

问题描述

大家好,


我正在使用班级通过

网络传输一些数据。然后我将[Serializable]属性添加到了

类。


我的问题是这个类是框架的一部分而且

,我不希望开发人员调用空构造函数。

但是当我尝试

序列化这个类时,运行时发送给我一个例外,要求我提供一个

空构造函数如:


public MyClass(){}


有没有办法避免这种情况知道在ISerializable界面上使用

的问题,它仍然要求我实现一个空的构造函数吗?


感谢您的帮助。


Amadrias

解决方案

我不太确定,但我认为它甚至可以工作如果你将空的

构造函数定义为私有或受保护。


希望这会有所帮助,


Trev。

Amadrias <是****** @ wanadoo.fr>在消息中写道

news:03 **************************** @ phx.gbl ... < blockquote class =post_quotes>大家好,

我正在使用一个类通过
网络传输一些数据。然后我将[Serializable]属性添加到
类。

我的问题是这个类是框架的一部分而且我不希望开发人员调用空构造函数。
但是当我尝试序列化这个类时,运行时发送给我一个例外,要求我提供一个空的构造函数,例如:

public MyClass( ){}

有没有办法避免这个问题,知道使用ISerializable接口,它仍然要求我实现一个空的构造函数?

感谢您的帮助。

Amadrias



您可以编写自己的序列化器和反序列化器,但这将是

相当多的工作...


-

Greetz,

Jan
________________________

阅读我的博客: http:// weblogs.asp.net/jan


" Amadrias" <是****** @ wanadoo.fr> schreef in bericht

news:03 **************************** @ phx.gbl ... < blockquote class =post_quotes>大家好,

我正在使用一个类通过
网络传输一些数据。然后我将[Serializable]属性添加到
类。

我的问题是这个类是框架的一部分而且我不希望开发人员调用空构造函数。
但是当我尝试序列化这个类时,运行时发送给我一个例外,要求我提供一个空的构造函数,例如:

public MyClass( ){}

有没有办法避免这个问题,知道使用ISerializable接口,它仍然要求我实现一个空的构造函数?

感谢您的帮助。

Amadrias



不幸的是,如果您将构造函数声明为私有

或受保护,它不能被.Net框架调用

然后它不起作用...


仍然需要空构造函数...


无论如何谢谢...


Amadrias

---- - 原创讯息-----
我'我不完全确定,但我认为即使
你将emptyconstructor定义为私有或受保护也会有效。

希望这会有所帮助,

Trev。

Amadrias <是****** @ wanadoo.fr>在消息中写道
新闻:03 **************************** @ phx.gbl ...

大家好,

我正在使用一个类通过
网络传输一些数据。然后我将[Serializable]属性添加到
类中。

我的问题是这个类是框架
的一部分,我不希望开发人员调用空
构造函数。但是当我尝试序列化这个类要求我为它提供一个空构造函数时,运行时发送给我一个例外:

public MyClass(){}
谢谢你帮助。

Amadrias





Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias

解决方案

I''m not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.
"Amadrias" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...

Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias



You could write your own serializer and de-serializer, but this would be
quite some work...

--
Greetz,
Jan
________________________
Read my weblog: http://weblogs.asp.net/jan

"Amadrias" <am******@wanadoo.fr> schreef in bericht
news:03****************************@phx.gbl...

Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias



Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn''t work...

Still needs the empty constructor...

Thanks anyway...

Amadrias

-----Original Message-----
I''m not exactly sure, but I think it will work even if you define the emptyconstructor as private or protected.

Hope this helps,

Trev.
"Amadrias" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...

Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the class.

My problem is that this class is part of a framework and that I do not want developers to call empty constructors. But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.



这篇关于基本类构造函数和序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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