我正在将一个库从 .net Framework 4.7 迁移到 .net core 2.2,并发现了深度对象克隆的问题,我将其范围缩小到下面的一个简短的可重现代码片段。
自己尝试一下:
.net框架4.7.2(通过)
.net core 2.2(失败)
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Test
{
public class Program
{
public static void Main(string[] args)
{
// any zone here, don't care what it is
var zone = TimeZoneInfo.GetSystemTimeZones()[0];
var formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, zone);
stream.Seek(0, SeekOrigin.Begin);
var result = formatter.Deserialize(stream);
Console.WriteLine("all ok");
}
}
}
}
在使用 .net core 2.2 的 Windows 平台上,这工作正常,但在 Linux 平台上出现异常:
Unhandled Exception: System.Runtime.Serialization.SerializationException: An error occurred while deserializing the object. The serialized data is corrupt. ---> System.ArgumentOutOfRangeException: The Month parameter must be in the range 1 through 12.
Parameter name: month
at System.TimeZoneInfo.TransitionTime.ValidateTransitionTime(DateTime timeOfDay, Int32 month, Int32 week, Int32 day, DayOfWeek dayOfWeek)
at System.TimeZoneInfo.TransitionTime.System.Runtime.Serialization.IDeserializationCallback.OnDeserialization(Object sender)
--- End of inner exception stack trace ---
at System.TimeZoneInfo.TransitionTime.System.Runtime.Serialization.IDeserializationCallback.OnDeserialization(Object sender)
at System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent()
问题:如何TimeZoneInfo在 Linux 中的 .net core 2.2 中使用二进制序列化?
梵蒂冈之花
相关分类