system.numerics.complex(单声道环境)的序列化错误

本文关键字:环境 序列化 错误 numerics complex 单声道 system 声道 | 更新日期: 2023-09-27 18:31:59

我有一个关于 MONO 中复数序列化的错误。如果我在 Microsoft .NET 环境中运行相同的代码,则根本没有错误。

我正在使用二进制格式化程序,这是使用它的函数:

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.");
    }

我在使用单声道运行时遇到了此错误(在带有"mono-sgen"的 linux 下):

未处理的异常:System.Runtime.Serialization.SerializationException:类型 System.Numerics.Complex 未标记为可序列化。 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

但是如果我用Visual Studio(2013)编译它,正如我所说,我完全没有问题。

我不是 C# 专家,两周前我开始编码,所以我不知道如何解决这个问题。我尝试创建一个从system.numerics.complex派生的自定义Complex类(标记为可序列化),但是.NET对此解决方案不满意,因为system.numerics.complex是一个密封类。

提前感谢,浮士德

system.numerics.complex(单声道环境)的序列化错误

看起来像单声道中的一个错误。Mono 没有 System.Numerics.Complex 类型的[Serializable]属性,而 MS .NET 有。

系统.数字.复杂 来自单声道源

系统.数字.复杂 来自 MS.NET 来源

作为解决方法,您可以下载其中一个文件,添加[Serializable]属性,然后使用条件编译指令将文件添加到项目中。( #if MONO ...源文件在这里... #endif )。然后将MONO添加到解决方案配置文件中,这将定义项目设置中的MONO。在这种情况下,您必须遵守两个不同的.exe文件 - 一个用于 MS.NET 平台,另一个用于 Mono 平台,但至少序列化应该有效。