Created
August 1, 2017 09:09
-
-
Save huseyint/8c8e216d1a7e87d756da3e9a9fcaf239 to your computer and use it in GitHub Desktop.
C# BinaryFormatter works fine with readonly fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
class Program | |
{ | |
static void Main() | |
{ | |
var formatter = new BinaryFormatter(); | |
var stream = new MemoryStream(); | |
formatter.Serialize(stream, new Foo("test")); | |
stream.Position = 0; | |
var foo = (Foo)formatter.Deserialize(stream); | |
Debug.Assert(foo.Bar == "test"); | |
} | |
} | |
[Serializable] | |
class Foo | |
{ | |
private readonly string _bar; | |
public Foo(string bar) | |
{ | |
_bar = bar; | |
} | |
public string Bar => _bar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment