Serialization will not deserialize delegates to non-public methods error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rene

    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.


  • Ansil MCAD

    #2
    RE: Serialization will not deserialize delegates to non-public methods

    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:
    [color=blue]
    > 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.
    >
    >
    >[/color]

    Comment

    • Sahil Malik

      #3
      Re: Serialization will not deserialize delegates to non-public methods error

      Rene,

      Please see


      - Sahil Malik



      "Rene" <nospam@nospam. com> wrote in message
      news:edMd$fCCFH A.1260@TK2MSFTN GP12.phx.gbl...[color=blue]
      >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.
      >[/color]


      Comment

      • Rene

        #4
        Re: Serialization will not deserialize delegates to non-public methods

        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" <AnsilMCAD@disc ussions.microso ft.com> wrote in message
        news:6DC300C2-332A-486E-B97F-EF5B8003EAAE@mi crosoft.com...[color=blue]
        > 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:
        >[color=green]
        >> 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.
        >>
        >>
        >>[/color][/color]


        Comment

        • Rene

          #5
          Re: Serialization will not deserialize delegates to non-public methods

          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" <nospam@nospam. com> wrote in message
          news:u%23a89VDC FHA.1408@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > 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" <AnsilMCAD@disc ussions.microso ft.com> wrote in message
          > news:6DC300C2-332A-486E-B97F-EF5B8003EAAE@mi crosoft.com...[color=green]
          >> 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:
          >>[color=darkred]
          >>> 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.
          >>>
          >>>
          >>>[/color][/color]
          >
          >[/color]


          Comment

          • Rene

            #6
            Re: Serialization will not deserialize delegates to non-public methods

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


            "Rene" <nospam@nospam. com> wrote in message
            news:e7a$baDCFH A.3976@tk2msftn gp13.phx.gbl...[color=blue]
            > 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" <nospam@nospam. com> wrote in message
            > news:u%23a89VDC FHA.1408@TK2MSF TNGP10.phx.gbl. ..[color=green]
            >> 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" <AnsilMCAD@disc ussions.microso ft.com> wrote in message
            >> news:6DC300C2-332A-486E-B97F-EF5B8003EAAE@mi crosoft.com...[color=darkred]
            >>> 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.
            >>>>
            >>>>
            >>>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            Working...