none
Cannot Serialize objects from classes derived from INotifyPropertyChanged

    Question

  • Hi all,

    this is my problem:

    I have a class "Szenario" that is declared like this:

    [Serializable()]
      public class Szenario : INotifyPropertyChanged {
       public event PropertyChangedEventHandler PropertyChanged;
    
    // some code for Properties, private fields and methods
    
       protected virtual void OnPropertyChanged(string propertyName) {
         if (PropertyChanged != null)
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
    }
    

     

    This class creates similar classes in its constructor. It contains some Properties bound to controls by Binding. The object to serialize is a CurrentItem of an ObservableCollection<Szenario>. All Binding works flawlessly.

    When trying to serialize an object of that class after it has been updated at least once in either direction thru Binding the serializer throws the exception:

     


    Type 'System.ComponentModel.PropertyChangedEventManager' in Assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

    The PropertyChanged delegate in fact contains a Property pointing to the EventManager mentioned in the throw-message after the first update of any bound Property.

    Is there anybody who can help?


    Regards Uwe
    Monday, January 17, 2011 12:11 AM

Answers

  • Hello Reed,

    thank you for your help. I have tried the following

    [NonSerialized()]
    public event PropertyChangedEventHandler PropertyChanged;
    
    
    
    Unfortunately the compiler does not like this construct. It diagnoses: "Error    1    Attribute 'NonSerialized' is not valid on this declaration type. It is only valid on 'field' declarations. "


    Do you know an alternate syntax to specify the Event more like a field?


    Regards Uwe

    Uwe,

     

    With events, the syntax required is:

     

    [field:NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by UwePfalz Monday, January 17, 2011 7:46 PM
    Monday, January 17, 2011 6:01 PM
    Moderator

All replies

  • You should most likely flag your PropertyChanged event with [NotSerialized].

     

    If the main usage is to serialize out of, back into, a WPF application, the event manager will resubscribe when the object is added back into the observable collection.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 17, 2011 12:30 AM
    Moderator
  • Hello Reed,

    thank you for your help. I have tried the following

    [NonSerialized()]
    public event PropertyChangedEventHandler PropertyChanged;
    
    
    
    Unfortunately the compiler does not like this construct. It diagnoses: "Error    1    Attribute 'NonSerialized' is not valid on this declaration type. It is only valid on 'field' declarations. "


    Do you know an alternate syntax to specify the Event more like a field?


    Regards Uwe
    Monday, January 17, 2011 9:27 AM
  • You can use [Field] attribute to your PropertyChangedEventHandler.

    [Field]
    public event PropertyChangedEventHandler PropertyChanged;
    
    

    IGNOU BUZZ
    ANALEC Infotech Pvt. Ltd.
    • Edited by Deepak Bhardwaj Monday, January 17, 2011 9:33 AM Correct event name
    • Proposed as answer by Deepak Bhardwaj Monday, January 17, 2011 9:42 AM
    Monday, January 17, 2011 9:32 AM
  • Hello Reed,

    thank you for your help. I have tried the following

    [NonSerialized()]
    public event PropertyChangedEventHandler PropertyChanged;
    
    
    
    Unfortunately the compiler does not like this construct. It diagnoses: "Error    1    Attribute 'NonSerialized' is not valid on this declaration type. It is only valid on 'field' declarations. "


    Do you know an alternate syntax to specify the Event more like a field?


    Regards Uwe

    Uwe,

     

    With events, the syntax required is:

     

    [field:NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by UwePfalz Monday, January 17, 2011 7:46 PM
    Monday, January 17, 2011 6:01 PM
    Moderator