Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 protobuf net中没有为System.Version定义序列化程序_C# 4.0_Protobuf Net - Fatal编程技术网

C# 4.0 protobuf net中没有为System.Version定义序列化程序

C# 4.0 protobuf net中没有为System.Version定义序列化程序,c#-4.0,protobuf-net,C# 4.0,Protobuf Net,在更新到最新的protobuf net(2.0.0.601)之后,我现在在尝试序列化System.Version类型的值时遇到一个异常 [ProtoContract(SkipConstructor=true)] public class ServerLoginInfo { [ProtoMember(1)] public Version ServerVersion { get; set; } } 在2.0.0.480版本中,它可以正常工作,无需执行任何特殊操作 是否有可能在

在更新到最新的protobuf net(2.0.0.601)之后,我现在在尝试序列化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;
然后存储模型(并重用它),然后使用model.Serialize/model.Deserialize

如果我更改此默认模型限制,则只需:

RuntimeTypeModel.Default.AllowParseableTypes = true;

我建议改为使用版本的代理,因为启用AllowParseableTypes选项可能会产生副作用,即禁用应用于AllowParseableTypes的其他类型的某些配置

详情如下:

为此,请添加以下初始值设定项:

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);
    }
}   

谢谢你的快速回复。我现在可能会坚持使用protobuf net的旧版本,因为更改需要很多更改<代码>RuntimeTypeModel.Default.AllowParseableTypes=true将使它在未来的版本中更容易。另外,谢谢你提供了一个很棒的图书馆:)@andy我打算今天晚些时候做些修改——我看看能不能把它挤进去,听起来不错。再次感谢@MarcGravell@andy.er602包括这一更改,现在可以使用了。好的,我将在下一版本中试用新版本。谢谢你的信息。