0
votes

I have an error about serialization of complex numbers in MONO. If I run the same code in a Microsoft .NET environment I have no error at all.

I'm using a binary formatter, and this is the function using it:

public static void dump_to_file(List<QuantumOperator> obj, string custom_filename = "")
    {
        if (custom_filename.Length > 0)
        {
            custom_filename = "-" + custom_filename;
        }
        var filename = filename_prefix + "-" + filename_suffix + custom_filename + ".pickle";
        Console.WriteLine("Begin writing file: " + filename);
        var binform = new BinaryFormatter();
        var f = new FileStream(filename, FileMode.OpenOrCreate);
        var sw = Stopwatch.StartNew();
        binform.Serialize(f, obj);
        f.Close();
        sw.Stop();
        Console.WriteLine("Writing time: " + sw.Elapsed.TotalSeconds.ToString() + " seconds.");
        Console.WriteLine("Write done. Closing file.");
    }

I have this error running with MONO (under linux with "mono-sgen"):

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type System.Numerics.Complex is not marked as Serializable. at System.Runtime.Serialization.Formatters.Binary.BinaryCommon.CheckSerializable (System.Type type, ISurrogateSelector selector, StreamingContext context) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in :0

but if I compile it with Visual Studio (2013) I have no problem at all, as I said.

I'm not a C# expert, I'm started to code two weeks ago, so I have no idea how to fix this problem. I tried to create a custom Complex class (marked as serializable) derived from system.numerics.complex, but .NET is no happy with this solution, because system.numerics.complex is a sealed class.

thanks in advance, Fausto

1

1 Answers

0
votes

This looks like a bug in mono. Mono does not have [Serializable] attribute for System.Numerics.Complex type while MS .NET has.

System.Numerics.Complex from mono source

System.Numerics.Complex from MS.NET source

As a workaround you can download one of these files, add [Serializable] attribute and add the file to your project using conditional compilation instructions. (#if MONO ... source file here... #endif). Then add to your solution profile MONO, which defines MONO in project settings. In that case you'll have to complie two different .exe files - one for MS.NET platform and another one for Mono platform, but at least serialization should work.