0

I would like to serialize classed what potentially can have properties what's current value is an anonymous function. As far as I knew this does not work out ob the box. However my very first unit test seems to be pass:

[Test]
public void AnonymousTest()
{
    Func<double, double, double> func = (double a, double b) =>
    {
        if (a > 0) // Dummy if branch to make sure this is a delegate and not expression
        {
            return a + b;
        }
        return 0;
    };
    var persistedFunc = SaveAndLoad(func); // Serialize and deserialize with BinaryFormatter
    Assert.AreEqual(7, persistedFunc(3, 4)); // Green
}

My question: is this working just because the deserialized delegate is running in the very same assembly, where the anonymous function exist and exist with the very same compiler generated name, or can we expect it will work in the future with the potentially changed and recompiled assembly?

EDIT: To narrow the question: Do we know at all what are the prerequisites of this is working? Say could a simple future recompile render useless the persisted state?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • When you say "in the future", do you intend to save the serialized data and deserialize it in the future? I ask because the BinaryFormatter serialization is meant to be a *transport* format, not a *storage* format. There's all kinds of things that will make it unable to deserialize if you change your program. It is meant to be used as a way to transport objects between disparate and separate parts of your program, of the same version. – Lasse V. Karlsen Oct 28 '15 at 11:57
  • Anonymous delegate representation changed between the old native compiler and the new C# based Roslyn. You shouldn't rely on the format staying the same. – Yuval Itzchakov Oct 28 '15 at 11:59
  • @GazTheDestroyer: I've read that, and it do not discuss anonymous functions. It is clear for me, that delegated can be serialized with the exact same future compatibility restrictions like any other class. Obviously if the type changes, it can not deserialized from an older binary format – g.pickardou Oct 28 '15 at 12:00
  • @YuvalItzchakov: Supposing I will use the new compiler from now to compile this assembly. – g.pickardou Oct 28 '15 at 12:01
  • @g.pickardou I wouldn't rely on that. Check Mark Gravells answer which talks about serializing expression trees instead. – Yuval Itzchakov Oct 28 '15 at 12:03
  • @LasseV.Karlsen: True, but the problem is not about the tool (BinaryFormatter) but the task. The task itself involves the problem you are exposed: The program are changing, still some classes ('configurations') must be stored, but unfortunately this can contain delegates, even anonymous ones – g.pickardou Oct 28 '15 at 12:04
  • I was not trying to answer your question, I was just making sure you knew about the risks of the BinaryFormatter class and storage. Serializing delegates should just not be done, period. It will never be safe due to the way anonymous methods are named. – Lasse V. Karlsen Oct 28 '15 at 12:21

0 Answers0