Serialization will not deserialize delegates to non-public methods error

R

Rene

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 (PrivateFunction);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunction" 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.
 
G

Guest

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:NonSerialized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum
 
R

Rene

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:NonSerialized] 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 said:
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:NonSerialized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

Rene said:
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 (PrivateFunction);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunction" 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.
 
R

Rene

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 said:
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:NonSerialized] 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 said:
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:NonSerialized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

Rene said:
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 (PrivateFunction);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunction" 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.
 
R

Rene

I am so stupid, never mind, I figured out what is going on. Nothing to do
with .Net, just my own implementation.


Rene said:
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 said:
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:NonSerialized] 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 said:
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:NonSerialized]
public event EventHandler MyEvent;
}

regards
Ansil
Trivandrum

:

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 (PrivateFunction);

Life is good when I serialize the object but things go to hell when I
deserialize it. What a hell? If I make the "PrivateFunction" 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top