简体   繁体   English

如何序列化数组?

[英]How to serialize arrays?

Symptom: No serializer defined for type: System.Array 症状:没有为类型定义序列化程序:System.Array

Since all C# arrays inherit from the Array class, I thought that this would be valid in ProtoBuf-net 由于所有C#数组都继承自Array类,我认为这在ProtoBuf-net中是有效的

[ProtoMember(1)]
public Array SomeKindOfArray = new int[]{1,2,3,4,5};

[ProtoMember(2)]
public List<Array> SomeKindOfList = new List<Array>();

Should I register Array with the RuntimeTypeModel? 我应该使用RuntimeTypeModel注册Array吗?

m.Add(typeof (Array), true);  // does not seem to help

Attempts: 尝试:

new int[]{1,2,3,4} // works

(object)new int[] { 1, 2, 3, 4 } // does not work

(Array)new int[] { 1, 2, 3, 4 } // does not work

Another possible solution (can't find the SO url right now): 另一种可能的解决方案(现在找不到SO url):

Have a list of a base type class items. 有一个基类型类项的列表。

Wrap each type. 包裹每种类型。 eg 例如

class TheBase{}

class ArrayOfInt { public int[] value;}

The would then become one of converting to and from a list of wrappers. 然后,它将成为转换到包装器列表之一。 Is there an easier way forward? 有更简单的方法吗?

protobuf-net really really wants to understand the data you are serializing; protobuf-net真的很想了解你正在序列化的数据; it isn't enough to have Array , as that can't map to any protobuf definition. 拥有Array是不够的,因为它无法映射到任何protobuf定义。 Repeated data (conceptually, arrays) has a very terse and very specific representation in the protobuf specification, which does not allow extra room for saying, on an individual basis, "of [x]". 重复数据(概念上,数组)在protobuf规范中具有非常简洁且非常具体的表示,这不允许额外的空间来单独说出“[x]”。 In protobuf, the "of [x]" is expected to be already known and fixed in advance . 在protobuf中,预计“[x]”的“ 预先知道并修复”。

So: 所以:

[ProtoMember(1)]
public int[] AnIntegerArray {get;set;}

[ProtoMember(2)]
public Customer[] ACustomerArray {get;set;}

will work fine. 会很好的。 But Array will not work at all. Array根本不起作用。

Depending on how important this is, there may be other options, for example (and you may want to tweak the names!): 根据它的重要程度,可能还有其他选项(例如,您可能需要调整名称!):

[ProtoContract]
[ProtoInclude(1, typeof(DummyWrapper<int>)]
[ProtoInclude(2, typeof(DummyWrapper<Customer>)]
public abstract class DummyWrapper {
    public abstract Type ElementType {get;}
}
[ProtoContract]
public class DummyWrapper<T> : DummyWrapper {
    [ProtoMember(1)]
    public T[] TheData {get;set;}

    public override Type ElementType {get { return typeof(T); } }
}

with: 有:

[ProtoMember(1)]
public DummyWrapper TheData {get;set;}

would work, I suspect (untested). 我怀疑(未经测试)会工作。 With classes, there is a little extra room that protobuf-net can use to implement inheritance (which also, technically, isn't supported in the protobuf specification - this is a shim that protobuf-net squeezes in). 对于类,protobuf-net可以使用一些额外的空间来实现继承(从技术上讲,在protobuf规范中也不支持 - 这是protobuf-net压缩的垫片)。

Why don't you try creating a new ISerializable object like this 为什么不尝试像这样创建一个新的ISerializable对象

[Serializable()]    
public class ArrayOfInt : ISerializable 
{
    public Array ....etc

and override the GetObjectData() from the interface ISerializable 并从ISerializable接口覆盖GetObjectData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM