Protobuf-net将字符串字段反序列化为c#guid

问题描述 投票:1回答:1

在简化的情况下,假设我们有一个自定义的c#类型,其中包含一个guid字段,并且相应的原型类型为]

message HelloReply {
  string message = 1;
  string guid_id = 2; // this is defined GUID in corresponding c# type
  repeated string names = 3;
  map<string, double> prices = 4;
}

[当我尝试将此原型反序列化为c#类型时,出现异常,指出'无效的线型'和指向explanation的链接,这对我没有帮助。有没有解决的办法,还是我忽略了什么?

c# protocol-buffers protobuf-net
1个回答
0
投票

Protobuf-net对向导有意见。这些意见是在很长时间内提出的,现在让我感到遗憾,但如果不打断人们,这些意见很难恢复。如果我今天回想起来写这篇文章,是的:它可能只是序列化为字符串。但是:这不是今天的期望!

坦率地说,我想用shadow属性来解决它。所以代替

[ProtoMember(42)]
public Guid Foo {get;set;}

您可以使用:

public Guid Foo {get;set;}
[ProtoMember(42)]
private string FooSerialized {
    get => Foo.ToString(); // your choice of formatting
    set => Foo = Guid.Parse(value);
}
© www.soinside.com 2019 - 2024. All rights reserved.