【问题标题】:'NotSupportedException: Type cannot be represented as a default value' after updating protobuf-net'NotSupportedException:类型不能表示为默认值'更新 protobuf-net 后
【发布时间】:2019-09-06 22:58:20
【问题描述】:

更新到最新版本的 protobuf-net (2.4.0) 后,一切都不再起作用了:

System.InvalidOperationException:它是 无法为以下各项准备序列化程序:QueryContainer

多年来,2.0.0.668 版本一切正常,似乎也适用于 2.2.1 版本,但 2.3.0 及更高版本导致此问题。

发生了什么变化,或者我从来没有做对什么? :-) 我构建了一个小型复制品,结果是:

无法为 ProtoRepro.Program+QueryContainer 准备序列化程序 InnerException:类型不能表示为默认值:System.Collections.Generic.KeyValuePair`2[[ProtoRepro.Program+Query, ProtoRepro, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Double, System.Private.CoreLib,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e]]

using ProtoBuf;
using ProtoBuf.Meta;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace ProtoRepro {

  internal class Program {
    private static void Main(string[] args) {
      try {
        RuntimeTypeModel.Default[typeof(QueryContainer)].CompileInPlace();
        // we get here with version 2.2.1 and lower
        Console.WriteLine("Everything is fine!");
      } catch (InvalidOperationException ioex) {
        Console.Write(ioex.ToString());
      } catch (NotSupportedException nsex) {
        Console.Write(nsex.ToString());
      }
    }

    [Serializable, ProtoContract]
    public class QueryContainer {
      private QueryContainer() { }
      [ProtoMember(1, OverwriteList = true)]
      public Dictionary<string, KeyValuePair<Query, double>> QueryAndWeightPerVariable { get; protected set; }
    }

    [Serializable, ProtoContract]
    public class Query {
      [ProtoMember(1)]
      public QueryValue QueryValue { get; set; }
      protected Query() { }
      public Query(QueryValue queryValue) {
        QueryValue = queryValue;
      }
    }

    [Serializable, ProtoContract]
    public class QueryValue {
      [ProtoMember(1)]
      public object Value { get; set; }
      protected QueryValue() { }
      public QueryValue(object value) {
        Value = value;
      }
    }
  }
}

【问题讨论】:

  • @mjwills 对于重现来说没关系,但不,在实际代码中它一直受到保护,现在又受到保护。 Query 的无参数构造函数现在也受到保护,而不是公共的。显然我在这方面工作太晚了;-)

标签: c# protobuf-net


【解决方案1】:

我将不得不设置一个本地测试来深入了解它,但这里的更改很可能与“地图”行为有关。因此,您可以尝试添加

[ProtoMap(DisableMap = true)]

到字典成员。如果这是一个猜测,我深表歉意(特别是如果我错了!),但深入研究它必须等到我有时间。将其作为问题记录在 GitHub 上也可能很有用。

【讨论】:

  • 这有帮助,非常感谢!也无需道歉; protobuf-net 很棒,您的支持水平和速度都很棒,谢谢!
猜你喜欢
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多