对象(通用)的 Protobuf-net 序列化抛出错误没有为类型定义序列化程序:System.Object [英] Protobuf-net serialization of object (generic) throws error No serializer defined for type: System.Object

查看:68
本文介绍了对象(通用)的 Protobuf-net 序列化抛出错误没有为类型定义序列化程序:System.Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Protobuf-net 序列化一个 generic(T) 对象,该对象可以保存任何类型的数据(int/string/DateTime).以下是我的代码

How to serialize a generic(T) object which can hold any type of data (int / string / DateTime) using Protobuf-net. Following is my code

[ProtoContract]
public class E1DataRow
{
  [ProtoMember(1)]
  public List<NameValue> NameValues { get; set; }

  public E1DataRow()
  {
    NameValues = new List<NameValue>();
  }

  public void AddNameValue(string name, object obj, Type type)
  {
      NameValues.Add(new NameValue { Name = name, Value = obj, ValueType = type });
  }
}


[ProtoContract]
public class NameValue
{
  [ProtoMember(1)]
  public string Name { get; set; }
  [ProtoMember(2)]
  public object Value { get; set; }
  [ProtoMember(3)]
  public Type ValueType { get; set; }
}

序列化代码

var e1DataRows = new List<E1DataRow>();
/*
Code to add Data rows to e1DataRows
e1DataRow.AddNameValue(column.ColumnName, value, column.TypeOfColumn);
*/
using (var stream = File.OpenWrite(path))
{
  Serializer.Serialize(stream, e1DataRows);
}

[ProtoMember(2, DynamicType = true)]
public object Value { get; set; }

以上代码抛出以下错误(DynamicType = true)ProtoMemberAttribute.DynamicType' 已过时:'Reference-tracking 和 dynamic-type 目前未在此版本中实现;它们可能会在以后恢复;这部分是由于怀疑这些功能是否值得推荐,部分是由于对测试所有场景过于自信(这需要时间;那个时间还没有发生);邀请反馈'

Above code throws following error (DynamicType = true) ProtoMemberAttribute.DynamicType' is obsolete: 'Reference-tracking and dynamic-type are not currently implemented in this build; they may be reinstated later; this is partly due to doubts over whether the features are adviseable, and partly over confidence in testing all the scenarios (it takes time; that time hasn't get happened); feedback is invited'

如果您能帮助您使用 Protobug-net 序列化列表,那就太好了.谢谢...

It would be great if you can help with how to serialize a List using Protobug-net. Thanks...

推荐答案

你可能想看看 https://github.com/dotarj/protobuf-net-data - 这不隶属于 protobuf-net(不同的作者等),但它使用 protobuf-net执行 DataTable 和数据读取器的序列化,所以它可能做你想要的现成的.

You may want to look at https://github.com/dotarj/protobuf-net-data - this isn't affiliated with protobuf-net (different authors etc), but it uses protobuf-net to perform serialization of DataTable and data-readers, so it might do what you want ready-made.

至于自己实现:

protobuf-net 从根本上不支持 object(或 dynamic,这只是一种拼写 object 的奇特方式).有一些方法可以解决这个问题,本质上类似于 protobuf 中的 oneof 处理 - 即类似(在 protobuf 术语中):

protobuf-net does not support object (or dynamic, which is just a fancy way of spelling object), fundamentally. There are ways of working around this, essentially similar to the oneof handling in protobuf - i.e. something like (in protobuf terms):

message Foo {
    oneof payload {
        string payload_string = 1;
        bool payload_bool = 2;
        int32 payload_int32 = 3;
        float payload_float = 4;
        // etc
    }
}

由于条件序列化",这很容易在 protobuf-net 中组合在一起,这意味着您可以执行以下操作:

This is pretty easy to put together in protobuf-net thanks to "conditional serialization", which means you could do something like:

public object Value { get; set; }

[ProtoMember(1)]
public string ValueString
{
    get => (string)Value;
    set => Value = value;
}
public bool ShouldSerializeValueString()
    => Value is string;

[ProtoMember(2)]
public string ValueBoolean
{
    get => (bool)Value;
    set => Value = value;
}
public bool ShouldSerializeValueBoolean()
    => Value is string;

// etc

这篇关于对象(通用)的 Protobuf-net 序列化抛出错误没有为类型定义序列化程序:System.Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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