使用.NET Standard和.NET Framework之间的BinaryFormatter进行序列化和反序列化

时间:2018-01-19 05:12:15

标签: c# serialization .net-core .net-standard

我在.NET Standard 2类库中有一个类:

[Serializable]
public class CustomObject
{
    public Dictionary<DayOfWeek, decimal> Dictionary { get; set; }
}

我在.NET Core控制台应用程序中使用BinaryFormatter对其进行序列化:

class Program
{
    static void Main(string[] args)
    {
        var binaryFormatter = new BinaryFormatter();

        var customObject = new CustomObject { Dictionary = new Dictionary<DayOfWeek, decimal> { { DayOfWeek.Friday, 100m } } };

        using (var memoryStream = new MemoryStream())
        {
            binaryFormatter.Serialize(memoryStream, customObject);

            File.WriteAllBytes(@"C:\temp\serializedObject.dat", memoryStream.ToArray());
        }
    }
}

如果我在.NET Framework控制台应用程序中反序列化它:

class Program
{
    static void Main(string[] args)
    {
        var bytes = File.ReadAllBytes(@"C:\temp\serializedObject.dat");

        var binaryFormatter = new BinaryFormatter();

        using (var memoryStream = new MemoryStream(bytes))
        {
            var customObject = (CustomObject) binaryFormatter.Deserialize(memoryStream);

            var keyCount = customObject.Dictionary.Keys.Count;

        }
    }
}

我收到错误:

  

System.Runtime.Serialization.SerializationException:'无法加载类型System.Collections.Generic.Dictionary`2 [[System.DayOfWeek,System.Private.CoreLib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e ],[System.Decimal,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]反序列化所需。'

所以我创建了一个自定义活页夹:

public class CustomBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        if (typeName.Equals(
            "System.Collections.Generic.Dictionary`2[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
        )
        {
            return typeof(Dictionary<DayOfWeek, decimal>);
        }

        return Type.GetType($"{typeName}, {assemblyName}");
    }
}

之后,我收到以下错误:

  

System.Runtime.Serialization.SerializationException:'具有ID 5的对象在修正中被引用但不存在。'

所以我将另一个转换添加到序列化活页夹:

public class CustomBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        if (typeName.Equals(
            "System.Collections.Generic.Dictionary`2[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
        )
        {
            return typeof(Dictionary<DayOfWeek, decimal>);
        }

        if (typeName.Equals(
            "System.Collections.Generic.KeyValuePair`2[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
        )
        {
            return typeof(KeyValuePair<DayOfWeek, decimal>);
        }

        return Type.GetType($"{typeName}, {assemblyName}");
    }
}

并收到以下错误:

  

System.InvalidCastException:'对象必须实现IConvertible。'

通过活页夹的所有类型都是:

SharedLib.CustomObject, SharedLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.Collections.Generic.Dictionary`2[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.Generic.EnumEqualityComparer`1[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.Generic.KeyValuePair`2[[System.DayOfWeek, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.DayOfWeek, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

如何将其反序列化?

0 个答案:

没有答案