none
How to mark PropertyChanged Event as NonSerialized RRS feed

  • Question

  • I have an issue with the PropertyChanged event being automatically added to SQL objects dropped from the server explorer.  I'm returning some of these objects to a Windows Forms application via a service reference, and storing the objects on the local machine using binary serialization.

    I'm binding a list of an object to a WPF user control to display to the user, and allow the user to edit some of the fields.  When I go to save the object using the BinaryFormatter, I'm getting the following error:

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

    From what I've read, the PropertyChanged event needs the [Field:NonSerialized] attribute.  Is there any way to tell the LINQ designer to mark this attribute on the event without tweaking the designer class by hand?  If not, is there any way to mark this event as non serializable in the reference.cs file on the client?  Or, is there any other way to tell the BinaryFormatter to ignore this during serialization?

    Thanks!

    EJ
    Tuesday, August 4, 2009 11:31 PM

Answers

  • This seems like an issue with the Service Reference code generator than LINQ to SQL, since it's an issue on your client side. I found something on Connect that says how you can stop the PropertyChanged event being generated:

    If you look in your client project directory, there is a directory called Service References which contains a directory per service reference. Inside this directories there is a file called Reference.svcmap that contains the proxy generation configuration. Open the file and change the line that says <EnableDataBinding>true</EnableDataBinding> to <EnableDataBinding>false</EnableDataBinding>

    Can you do this and then add the PropertyChanged event to a partial class and marked it NonSerialized there?
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by ejohn20 Tuesday, August 11, 2009 4:07 PM
    Sunday, August 9, 2009 10:20 PM
    Answerer

All replies

  • There's nothing built-in but you can grab a free template from http://l2st4.codeplex.com that lets you 100% tweak the code generation to what you want it to be :)

    [)amien
    Wednesday, August 5, 2009 3:32 AM
    Moderator
  • Unfortunately, marking the PropertyChanged event in the server side dbml designer does not resolve this issue.  When updating the service reference, the proxy class Reference.cs still auto generates the PropertyChanged event hander without the NonSerialized attribute.

    For now, I have manually modified the Reference.cs file to include the [field:NonSerialized] attribute for all PropertyChanged events.  Although, this is not a good solution as this will need to be done every time the service reference is updated.

    Let me know if anyone knows of a way to force this attribute to be skipped without manually modifying the Reference.cs file?
    Wednesday, August 5, 2009 6:51 PM
  • This seems like an issue with the Service Reference code generator than LINQ to SQL, since it's an issue on your client side. I found something on Connect that says how you can stop the PropertyChanged event being generated:

    If you look in your client project directory, there is a directory called Service References which contains a directory per service reference. Inside this directories there is a file called Reference.svcmap that contains the proxy generation configuration. Open the file and change the line that says <EnableDataBinding>true</EnableDataBinding> to <EnableDataBinding>false</EnableDataBinding>

    Can you do this and then add the PropertyChanged event to a partial class and marked it NonSerialized there?
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by ejohn20 Tuesday, August 11, 2009 4:07 PM
    Sunday, August 9, 2009 10:20 PM
    Answerer
  • Good find!!  After changing this property, the generated classes no longer inherit from INotifyPropertyChanged.  This removes all PropertyChanged events, and its references.

    The WPF user control still seems to work correctly without this event being present.
    Tuesday, August 11, 2009 4:11 PM