在更新到最新的ProtoBuf-net(2.0.0.601)之后,我现在在尝试序列化type system.version的值时会得到异常。
[ProtoContract(SkipConstructor=true)]
public class ServerLoginInfo
{
[ProtoMember(1)]
public Version ServerVersion { get; set; }
}
这曾经在2.0.0.480中正常工作,而无需做任何特别的事情。
是否可以在新版本中使其正常工作,还是我唯一的回滚旧版本的选择?
(我还需要序列化/避难所与旧的Protobuf-net版本兼容。)
我怀疑这与allowparseabletypes有关 - 即是否将静态解析方法视为后备。如果我记得,不能在默认模型上启用此功能,但是我想我会在下一个部署中删除该限制。但是现在:
var model = TypeModel.Create();
model.AllowParseableTypes = true;
然后存储模型(并重复使用),然后使用模型。Serialize/model.Deserialize。
如果我更改此默认模式限制,则只需:
RuntimeTypeModel.Default.AllowParseableTypes = true;
我建议使用替代版本的版本,因为启用allowparseabletypes选项可以具有禁用某些配置的副作用。
在此处提供更多详细信息:https://github.com/mgravell/protobuf-net/issues/183
要这样做,添加以下初始器:
RuntimeTypeModel.Default.Add(typeof(Version), false).SetSurrogate(typeof(VersionSurrogate));
和以下类型:
[ProtoContract]
public class VersionSurrogate
{
[ProtoMember(1)]
public string Version { get; set; }
public static implicit operator VersionSurrogate(Version value)
{
return new VersionSurrogate
{
Version = value.ToString()
};
}
public static implicit operator Version(VersionSurrogate value)
{
return System.Version.Parse(value.Version);
}
}