有没有一种方法使用IReadOnlyCollection< T> / IReadOnlyList< T>与protobuf网 [英] Is there a way to use IReadOnlyCollection<T>/IReadOnlyList<T> with protobuf-net

查看:708
本文介绍了有没有一种方法使用IReadOnlyCollection< T> / IReadOnlyList< T>与protobuf网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,在与集合protobuf网需求的GetEnumerator 进行序列化和类型与添加的工作反序列化。

I gather that when working with collections protobuf-net needs GetEnumerator for serialization and a type with Add for the deserialization.

对于没有类型的添加您可以设置反序列化之前,做一个继承类型。这适用于例如用的IEnumerable 列表

For types that don't have an Add you can set an inheriting type that does before deserialization. This works for example with IEnumerable and a List:

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Sheep
{
    public IEnumerable<string> Children { get; set; }
    public Sheep()
    {
        Children = new List<string>();
    }
}

var dolly = RuntimeTypeModel.Default.DeepClone(new Sheep
{
    Children = new[]
    {
        "Bonnie",
        "Sally",
        "Rosie",
        "Lucy",
        "Darcy",
        "Cotton"
    }
});

然而,当我做同样的 IReadOnlyCollection (或 IReadOnlyList ):

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Sheep
{
    public IReadOnlyCollection<string> Children { get; set; }
    public Sheep()
    {
        Children = new List<string>();
    }
}

我得到一个异常:

I get an exception:

无法解析合适的Add方法System.Collections.Generic.IReadOnlyCollection`1 [System.String,mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089]]

Unable to resolve a suitable Add method for System.Collections.Generic.IReadOnlyCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

那么,有没有一种方法,使 IReadOnlyCollection / IReadOnlyList 的protobuf网柄成员?

So, is there a way to make protobuf-net handle members of IReadOnlyCollection/IReadOnlyList?

编辑:支持IReadOnlyCollection:我已经为这个功能对项目的存储库中打开的问题/ IReadOnlyList成员

推荐答案

这里的主要问题是合并。对于非合并(或基本上是:用列表替换启用),我们也许可以硬codeA具体类型和策略,但没有明显的事情,否则做合并

The main problem here is "merge". For non-merge (or basically: with list replacement enabled), we could probably hard-code a concrete type and strategy, however, there is no obvious thing to do for merge otherwise.

有没有神奇的这种情况下,目前,任何解决方案都需要库的变化。

There is no magic for this scenario at the moment; any solution would require library changes.

这篇关于有没有一种方法使用IReadOnlyCollection&LT; T&GT; / IReadOnlyList&LT; T&GT;与protobuf网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆