0

When I write a class like this, it gets serialized/deserialized perfectly.

[Serializable]
public class Post
{
    public string Subject { get; set; }
}

When I use a private field however, deserialization of existing data fails. The following class no longer deserializes the subject

[Serializable]
public class Post
{
    private string subj;
    public string Subject { get { return subj; } set { subj = value; } }
} 

I'm using the following piece of code

var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, posts); 

and vice versa.

Can anybody explain why this happens?

dbc
  • 104,963
  • 20
  • 228
  • 340
Marlon
  • 45
  • 7
  • 2
    How are you serializing and how are you deserializing? – Camilo Terevinto Apr 03 '18 at 23:33
  • 2
    What serializer are you using? is it `BinaryFormatter` by any chance? Have you *changed* the field name and/or changed between a field+property to an auto-implemented-property (the thing shown in the first example). Also: **don't use `BinaryFormatter`**. Like... ever – Marc Gravell Apr 03 '18 at 23:37
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/ – Vijunav Vastivch Apr 04 '18 at 00:01
  • @CamiloTerevinto - I'm using the following piece of code var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bformatter.Serialize(stream, posts); and vice versa. – Marlon Apr 04 '18 at 00:28
  • Well, then, follow @MarcGravell's advice and **don't use `BinaryFormatter`**. Normally, NewtonSoft.Json is the best option – Camilo Terevinto Apr 04 '18 at 00:31
  • @MarcGravell - yes, I'm using BinaryFormatter, yes I have changed the field from a field with get/set to a field with get/set and a private field, but I haven't changed the field name. Also, what is wrong with BinaryFormatter? – Marlon Apr 04 '18 at 00:33
  • @CamiloTerevinto - I used BinaryFormatter because it doesn't seem logical to store 100s of Mbs of data in XML to me. Wouldn't XML or JSon add too much overhead (in tags etc...) to already big data files? – Marlon Apr 04 '18 at 00:34
  • @VijunavVastivch - thanks, I'll check these links out asap! – Marlon Apr 04 '18 at 00:34
  • For those cases you have Marc's excellent Protobuf.NET library: https://github.com/mgravell/protobuf-net – Camilo Terevinto Apr 04 '18 at 00:37
  • Take a look at [Binary Formatter and properties with\without backing fields](https://stackoverflow.com/q/29676594/3744182) which I think explains your problem and gives a possible workaround. (In fact this may be a duplicate; agree?) But it would be better if you **don't use `BinaryFormatter`** as others recommend. – dbc Apr 04 '18 at 03:02
  • @Marlon - you asked, *Also, what is wrong with BinaryFormatter?* See [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](http://stackoverflow.com/q/703073/3744182) – dbc Apr 04 '18 at 04:53
  • @Marlon indeed, changing from a manual field to an auto-prop will break `BinaryFormatter`'s assumptions – Marc Gravell Apr 04 '18 at 08:29
  • Thanks for all the answers! I understand where I went wrong now (as well as why BinaryFormatter is not an ideal choice). – Marlon Apr 04 '18 at 12:49

0 Answers0