如何在 c# 中序列化/反序列化不可变列表类型

本文关键字:反序列化 不可变 列表 类型 序列化 | 更新日期: 2023-09-27 18:32:19

如果我定义了类

[DataContract()]
class MyObject {
    [DataMember()]
    ImmutableList<string> Strings { get; private set}
}

ImmutableList<T>类型来自不可变库 https://www.nuget.org/packages/Microsoft.Bcl.Immutable。请注意,类 ImmutableList 没有默认构造函数或可变的 Add 方法。将内容添加到列表中采用表单。

myList = myList.Add("new string");

是否可以向 .NET 序列化机制添加一些自定义支持以支持此类型并演示如何反序列化它?

目前,该集合只是在反序列化时跳过,尽管可以对其进行序列化。

如何在 c# 中序列化/反序列化不可变列表类型

还有另一种干净的方法可以通过IDataContractSurrogate界面执行此操作。DataContractSerializer 允许您为不可序列化的对象提供代理项。下面是 ImmutableList<T> 的示例和测试用例。它使用反射,可能可以由比我更聪明的人进行优化,但它就在这里。

测试用例

using FluentAssertions;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using Xunit;
namespace ReactiveUI.Ext.Spec
{
    [DataContract(Name="Node", Namespace="http://foo.com/")]
    class Node
    {
        [DataMember()]
        public string Name;
    }
    [DataContract(Name="Fixture", Namespace="http://foo.com/")]
    class FixtureType
    {
        [DataMember()]
        public ImmutableList<Node> Nodes;
        public FixtureType(){
            Nodes = ImmutableList<Node>.Empty.AddRange( new []
            { new Node(){Name="A"}
            , new Node(){Name="B"}
            , new Node(){Name="C"}
            });
        }
    }

    public class ImmutableSurrogateSpec
    {  
        public static string ToXML(object obj)
            {
                var settings = new XmlWriterSettings { Indent = true };
                using (MemoryStream memoryStream = new MemoryStream())
                using (StreamReader reader = new StreamReader(memoryStream))
                using (XmlWriter writer = XmlWriter.Create(memoryStream, settings))
                {
                    DataContractSerializer serializer =
                      new DataContractSerializer
                          ( obj.GetType()
                          , new DataContractSerializerSettings() { DataContractSurrogate = new ImmutableSurrogateSerializer() }
                          );
                    serializer.WriteObject(writer, obj);
                    writer.Flush();
                    memoryStream.Position = 0;
                    return reader.ReadToEnd();
                }
            }
        public static T Load<T>(Stream data)
        {
            DataContractSerializer ser = new DataContractSerializer
                  ( typeof(T)
                  , new DataContractSerializerSettings() { DataContractSurrogate = new ImmutableSurrogateSerializer() }
                  );
            return (T)ser.ReadObject(data);
        }
        public static T Load<T>(string data)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
            {
                return Load<T>(stream);
            }
        }
        [Fact]
        public void ShouldWork()
        {
            var o = new FixtureType();
            var s = ToXML(o);
            var oo = Load<FixtureType>(s);
            oo.Nodes.Count().Should().Be(3);
            var names = oo.Nodes.Select(n => n.Name).ToList();
            names.ShouldAllBeEquivalentTo(new[]{"A", "B", "C"});
        }
    }
}

实现

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization;
namespace ReactiveUI.Ext
{
    class ImmutableListListConverter<T>
    {
        public static ImmutableList<T> ToImmutable( List<T> list )
        {
            return ImmutableList<T>.Empty.AddRange(list);
        }
        public static List<T> ToList(ImmutableList<T> list){
            return list.ToList();
        }
        public static object ToImmutable( object list )
        {
            return ToImmutable(( List<T> ) list);
        }
        public static object ToList(object list){
            return ToList(( ImmutableList<T> ) list);
        }
    }
    static class ImmutableListListConverter {

        static ConcurrentDictionary<Tuple<string, Type>, Func<object,object>> _MethodCache 
            = new ConcurrentDictionary<Tuple<string, Type>, Func<object,object>>();
        public static Func<object,object> CreateMethod( string name, Type genericType )
        {
            var key = Tuple.Create(name, genericType);
            if ( !_MethodCache.ContainsKey(key) )
            {
                _MethodCache[key] = typeof(ImmutableListListConverter<>)
                    .MakeGenericType(new []{genericType})
                    .GetMethod(name, new []{typeof(object)})
                    .MakeLambda();
            }
            return _MethodCache[key];
        }
        public static Func<object,object> ToImmutableMethod( Type targetType )
        {
            return ImmutableListListConverter.CreateMethod("ToImmutable", targetType.GenericTypeArguments[0]);
        }
        public static Func<object,object> ToListMethod( Type targetType )
        {
            return ImmutableListListConverter.CreateMethod("ToList", targetType.GenericTypeArguments[0]);
        }
        private static Func<object,object> MakeLambda(this MethodInfo method )
        {
            return (Func<object,object>) method.CreateDelegate(Expression.GetDelegateType(
            (from parameter in method.GetParameters() select parameter.ParameterType)
            .Concat(new[] { method.ReturnType })
            .ToArray()));
        }
    }
    public class ImmutableSurrogateSerializer : IDataContractSurrogate
    {
        static ConcurrentDictionary<Type, Type> _TypeCache = new ConcurrentDictionary<Type, Type>();
        public Type GetDataContractType( Type targetType )
        {
            if ( _TypeCache.ContainsKey(targetType) )
            {
                return _TypeCache[targetType];
            }
            if(targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(ImmutableList<>)) 
            {
                return _TypeCache[targetType] 
                    = typeof(List<>).MakeGenericType(targetType.GetGenericArguments());
            }
            else
            {
                return targetType;
            }
        }
        public object GetDeserializedObject( object obj, Type targetType )
        {
            if ( _TypeCache.ContainsKey(targetType) )
            {
               return ImmutableListListConverter.ToImmutableMethod(targetType)(obj);
            }
            return obj;
        }
        public object GetObjectToSerialize( object obj, Type targetType )
        {
            if ( targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(ImmutableList<>) )
            {
               return ImmutableListListConverter.ToListMethod(targetType)(obj);
            }
            return obj;
        }
        public object GetCustomDataToExport( Type clrType, Type dataContractType )
        {
            throw new NotImplementedException();
        }
        public object GetCustomDataToExport( System.Reflection.MemberInfo memberInfo, Type dataContractType )
        {
            throw new NotImplementedException();
        }

        public void GetKnownCustomDataTypes( System.Collections.ObjectModel.Collection<Type> customDataTypes )
        {
            throw new NotImplementedException();
        }

        public Type GetReferencedTypeOnImport( string typeName, string typeNamespace, object customData )
        {
            throw new NotImplementedException();
        }
        public System.CodeDom.CodeTypeDeclaration ProcessImportedType( System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit )
        {
            throw new NotImplementedException();
        }
        public ImmutableSurrogateSerializer() { }
    }
}

呵;我可以想象这里发生了什么...生成的代码可能正在执行(释义):

var list = obj.Strings;
while(CanReadNextItem()) {
    list.Add(ReadNextItem());
}

问题是BCL不可变API每次都需要您捕获结果,即

var list = obj.Strings;
while(CanReadNextItem()) {
    list = list.Add(ReadNextItem());
}
obj.Strings = list; // the private set is not a problem for this

预先存在的列表反序列化代码不能以这种方式工作,因为它从来不需要 - 事实上,Add有许多不同的实现,其中一些返回需要忽略的非 void 结果。

缺少非公共构造函数也可能使它有点不安,但如果这是主要问题,那么当它尝试创建非空列表时,我有点期待出现异常。

当然,在性能方面,list = list.Add(...) API 可能不是最合适的(尽管它应该可以工作)。

我最近写了关于这个主题的博客(在 protobuf-net 的上下文中,现在已经更新为使用这些集合类型): http://marcgravell.blogspot.co.uk/2013/09/fun-with-immutable-collections.html 希望这篇博客文章应该解释为什么这些差异意味着它不能很好地与现有的序列化技术配合使用,以及如何更新序列化库以处理这种情况。

要直接回答这个问题,我会说答案很简单:因为尚未对DataContractSerializer进行支持不可变集合所需的更改。我不知道是否有计划解决这个问题。但是:我很高兴地宣布:"在protobuf网络中工作";p

一种方法

是使用代理可变列表并使用 OnSerializing 和 OnDeserialized 钩子

[DataContract()]
class MyObject {
    public ImmutableList<string> Strings { get; private set}
    [DataMember(Name="Strings")]
    private List<String> _Strings;
    [OnSerializing()]
    public void OnSerializing(StreamingContext ctxt){
        _Strings = Strings.ToList();
    }
    [OnDeserialized()]
    public void OnDeserialized(StreamingContext ctxt){
        Strings = ImmutableList<string>.Empty.AddRange(_Strings);
    }
}

它不是很漂亮,但正如 Marc Gravell 在他的回答中指出的那样,DataContract 序列化器在不可变集合方面被破坏了,并且没有简单的钩子来教它如何在没有上述类型的黑客的情况下表现。

更新

数据协定序列化程序未损坏。有一种方法可以吸引代理人。请参阅此显示替代技术的单独答案。

https://stackoverflow.com/a/18957739/158285