Skip to content

Instantly share code, notes, and snippets.

@huseyint
Created August 1, 2017 09:09
Show Gist options
  • Save huseyint/8c8e216d1a7e87d756da3e9a9fcaf239 to your computer and use it in GitHub Desktop.
Save huseyint/8c8e216d1a7e87d756da3e9a9fcaf239 to your computer and use it in GitHub Desktop.
C# BinaryFormatter works fine with readonly fields
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