473,785 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization will not deserialize delegates to non-public methods error

I created a class (SomeClass), on that class, I declared a delegate (OnXyz)
and then declared an event based on that delegate (Xyz). All this in the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish about
having to have public methods on delegates? Why does serializing works but
not the opposite?

Thanks.
Nov 16 '05 #1
5 7054
hi
All delegates are compiled into serializable classes. This means that when
serializing an object that has a delegate member variable, the delegate's
internal invocation list is serialized too. This makes serializing delegates
very difficult because there are no guarantees that the target objects in the
internal list are serializable. Consequently, sometimes the serialization
will work and sometimes it will throw a serialization exception. In addition,
the object containing the delegate typically does not know or care about the
actual state of the delegate. This is even more the case when the delegate is
used to manage event subscriptions. The exact number and identity of the
subscribers are often transient values that should not persist between
application sessions.

As a result, you should mark delegate member variables as nonserializable
using the NonSerialized attribute:

[Serializable]
public class MyClass
{
[NonSerialized]
EventHandler m_MyEvent;
}

In the case of events, you must also add the field attribute qualifier when
applying the NonSerialized attribute so that the attribute is applied to the
underlying delegate rather than to the event itself:
[Serializable]
public class MyPublisher
{
[field:NonSerial ized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

"Rene" wrote:
I created a class (SomeClass), on that class, I declared a delegate (OnXyz)
and then declared an event based on that delegate (Xyz). All this in the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish about
having to have public methods on delegates? Why does serializing works but
not the opposite?

Thanks.

Nov 16 '05 #2
Rene,

Please see
http://codebetter.com/blogs/sahil.ma.../25/14510.aspx

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
"Rene" <no****@nospam. com> wrote in message
news:ed******** ******@TK2MSFTN GP12.phx.gbl...
I created a class (SomeClass), on that class, I declared a delegate (OnXyz)
and then declared an event based on that delegate (Xyz). All this in the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish about
having to have public methods on delegates? Why does serializing works but
not the opposite?

Thanks.

Nov 16 '05 #3
Hi Ansil:

I knew about this backing field (This delegate that is behind the event) and
when I first got the error the first thing I did was to add the
[field:NonSerial ized] attribute to the event declaration. Nonetheless, I
still got the error.

The thing is:

1) All of the objects that are subscribe to this event when I get the
error are serializables.

2) Why is object is serialized *without* any problems, but when its
desirelized it complains?

3) Why is it that if I make the function (target) that is subscribed to
the event public I don't get the error?

I think I am going to cry!!

"Ansil MCAD" <An*******@disc ussions.microso ft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com...
hi
All delegates are compiled into serializable classes. This means that when
serializing an object that has a delegate member variable, the delegate's
internal invocation list is serialized too. This makes serializing
delegates
very difficult because there are no guarantees that the target objects in
the
internal list are serializable. Consequently, sometimes the serialization
will work and sometimes it will throw a serialization exception. In
addition,
the object containing the delegate typically does not know or care about
the
actual state of the delegate. This is even more the case when the delegate
is
used to manage event subscriptions. The exact number and identity of the
subscribers are often transient values that should not persist between
application sessions.

As a result, you should mark delegate member variables as nonserializable
using the NonSerialized attribute:

[Serializable]
public class MyClass
{
[NonSerialized]
EventHandler m_MyEvent;
}

In the case of events, you must also add the field attribute qualifier
when
applying the NonSerialized attribute so that the attribute is applied to
the
underlying delegate rather than to the event itself:
[Serializable]
public class MyPublisher
{
[field:NonSerial ized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

"Rene" wrote:
I created a class (SomeClass), on that class, I declared a delegate
(OnXyz)
and then declared an event based on that delegate (Xyz). All this in the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish about
having to have public methods on delegates? Why does serializing works
but
not the opposite?

Thanks.

Nov 16 '05 #4
Dam it, I just notice that even when the object is deserialize without an
error, the events loose their subscription. I had a feeling this was going
to happen since the reference to the delegate would not be the same after
the object is deserialized.

OK, I am crying now..... there is no hope, is time for a hack.

"Rene" <no****@nospam. com> wrote in message
news:u%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Ansil:

I knew about this backing field (This delegate that is behind the event)
and when I first got the error the first thing I did was to add the
[field:NonSerial ized] attribute to the event declaration. Nonetheless, I
still got the error.

The thing is:

1) All of the objects that are subscribe to this event when I get the
error are serializables.

2) Why is object is serialized *without* any problems, but when its
desirelized it complains?

3) Why is it that if I make the function (target) that is subscribed
to the event public I don't get the error?

I think I am going to cry!!

"Ansil MCAD" <An*******@disc ussions.microso ft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com...
hi
All delegates are compiled into serializable classes. This means that
when
serializing an object that has a delegate member variable, the delegate's
internal invocation list is serialized too. This makes serializing
delegates
very difficult because there are no guarantees that the target objects in
the
internal list are serializable. Consequently, sometimes the serialization
will work and sometimes it will throw a serialization exception. In
addition,
the object containing the delegate typically does not know or care about
the
actual state of the delegate. This is even more the case when the
delegate is
used to manage event subscriptions. The exact number and identity of the
subscribers are often transient values that should not persist between
application sessions.

As a result, you should mark delegate member variables as nonserializable
using the NonSerialized attribute:

[Serializable]
public class MyClass
{
[NonSerialized]
EventHandler m_MyEvent;
}

In the case of events, you must also add the field attribute qualifier
when
applying the NonSerialized attribute so that the attribute is applied to
the
underlying delegate rather than to the event itself:
[Serializable]
public class MyPublisher
{
[field:NonSerial ized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

"Rene" wrote:
I created a class (SomeClass), on that class, I declared a delegate
(OnXyz)
and then declared an event based on that delegate (Xyz). All this in the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish about
having to have public methods on delegates? Why does serializing works
but
not the opposite?

Thanks.


Nov 16 '05 #5
I am so stupid, never mind, I figured out what is going on. Nothing to do
with .Net, just my own implementation.
"Rene" <no****@nospam. com> wrote in message
news:e7******** ******@tk2msftn gp13.phx.gbl...
Dam it, I just notice that even when the object is deserialize without an
error, the events loose their subscription. I had a feeling this was going
to happen since the reference to the delegate would not be the same after
the object is deserialized.

OK, I am crying now..... there is no hope, is time for a hack.

"Rene" <no****@nospam. com> wrote in message
news:u%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Ansil:

I knew about this backing field (This delegate that is behind the event)
and when I first got the error the first thing I did was to add the
[field:NonSerial ized] attribute to the event declaration. Nonetheless, I
still got the error.

The thing is:

1) All of the objects that are subscribe to this event when I get
the error are serializables.

2) Why is object is serialized *without* any problems, but when its
desirelized it complains?

3) Why is it that if I make the function (target) that is subscribed
to the event public I don't get the error?

I think I am going to cry!!

"Ansil MCAD" <An*******@disc ussions.microso ft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com...
hi
All delegates are compiled into serializable classes. This means that
when
serializing an object that has a delegate member variable, the
delegate's
internal invocation list is serialized too. This makes serializing
delegates
very difficult because there are no guarantees that the target objects
in the
internal list are serializable. Consequently, sometimes the
serialization
will work and sometimes it will throw a serialization exception. In
addition,
the object containing the delegate typically does not know or care about
the
actual state of the delegate. This is even more the case when the
delegate is
used to manage event subscriptions. The exact number and identity of the
subscribers are often transient values that should not persist between
application sessions.

As a result, you should mark delegate member variables as
nonserializable
using the NonSerialized attribute:

[Serializable]
public class MyClass
{
[NonSerialized]
EventHandler m_MyEvent;
}

In the case of events, you must also add the field attribute qualifier
when
applying the NonSerialized attribute so that the attribute is applied to
the
underlying delegate rather than to the event itself:
[Serializable]
public class MyPublisher
{
[field:NonSerial ized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

"Rene" wrote:

I created a class (SomeClass), on that class, I declared a delegate
(OnXyz)
and then declared an event based on that delegate (Xyz). All this in
the
same class. After that, I created another class where I instantiate my
previous class (SomeClass) and attach to its event like this:

someObject. Xyz += new SomeClass. OnXyz (PrivateFunctio n);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunctio n" a pulic
function everything appears to work fine. So what's this C# fetish
about
having to have public methods on delegates? Why does serializing works
but
not the opposite?

Thanks.



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1586
by: Silesian | last post by:
I realize that this is a newbie question but maybe someone will be able to tell me what am I doing wrong. createFile() gets called each time I have the user generate some numbers which then get added to the binlist ArrayList. I'm able to serialize and deserialize the Arraylist. However, when I deserialize the ArrayList, it will only show the first set of ints. I looked at the bin file and it does get appended to each time. Any help will...
4
1807
by: mdb | last post by:
I have a class (very simple int type data, as shown below) that I have serialized to disk. In my next version of the program, I have added some variables to that class. I'm expecting that this will break the serialization when I try to load the object from disk. I know there is something in .NET 2.0 to handle this, but how can I do it with .NET 1.1? Some sample code would be much appreciated... // The old version class Data
4
7487
by: Brian Keating | last post by:
wonder if anyone can help me here, i've a framework 1.1 dataset which i serialize in framework 1.1 and deserialize in framework 2.0. This is fine, problem is that i want to modify some of the records in framework 2.0 and serialize the data so framework 1.1 can deserialize it and do what it required. Is this possible?
7
9559
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects the entire type everytime it is invoked to serialize/deserialize an object of that type. Is there a way to prepare the binary formatter with a pre-defined type, such that it only reflects once but can be re-used to serialize/deserialize objects...
2
1859
by: Chukkalove | last post by:
I've had to change a class within an application. The previous developer used serialization to store objects to file. He didnt use ISerializable interface and even if he had..... I just dont trust or like using serialization just for the reasons Im asking for help about. He used binary serialization, and Im getting errors when the file is read from disk as the class has changed. I need to be able to read files created with previous...
15
2412
by: Jacques | last post by:
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...
2
1549
by: Saso | last post by:
Hi, I would like to serialize an instance of the bitmap instance. Actually, I've done serialization with the code below, but why parameter Tag is not serialized? I get the following exception after deserialization: Object reference is not set to an instance of an object. // Instance of bitmap. Bitmap B = new Bitmap(100, 100);
10
1523
by: Atmapuri | last post by:
Hi! I would like to deserialize an object to which other unknown objects hold multiple references. Is it possible to deserialize the object without the need to destroy and recreate it? How? Thanks! Atmapuri
3
1462
by: Marc | last post by:
Hi, I am trying to serialize a data structure -- a list (of custom class) -- in one application, then read it in with another application. My serialize and deserialize subs are in a module that is shared between the two applications, so they are using exactly the same code (this module also contains the class, so I am certain the class code is the same) The code I'm using is quite simple, so I'm not sure where I'm going wrong:
11
3695
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can send it over the wire. I'm just looking for how to serialize it, then pack it back up on the other end. Any help much appreciated.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.