【发布时间】: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