gpt4 book ai didi

c# - 判断C#中设置了哪个 'oneof' proto3字段

转载 作者:行者123 更新时间:2023-11-30 20:17:12 26 4
gpt4 key购买 nike

对于下面的 Protocol Buffer 消息(proto3),我如何确定设置了哪种类型?似乎没有像生成的 C++ 版本那样的“has_reply”方法。

message Event {
oneof type {
Connection connection = 1;
StatusReply reply = 2;
Error error = 3;
End end = 4;
Empty empty = 5;
};
}

最佳答案

https://developers.google.com/protocol-buffers/docs/reference/csharp-generated#oneof建议 TypeOneofCase 会告诉您设置了哪个:

Oneof Fields

Each field within a oneof has a separate property, like a regular singular field. However, the compiler also generates an additional property to determine which field in the enum has been set, along with an enum and a method to clear the oneof. For example, for this oneof field definition

oneof avatar {
string image_url = 1;
bytes image_data = 2;
}

The compiler will generate these public members:

enum AvatarOneofCase
{
None = 0,
ImageUrl = 1,
ImageData = 2
}

public AvatarOneofCase AvatarCase { get; }
public void ClearAvatar();
public string ImageUrl { get; set; }
public ByteString ImageData { get; set; }

If a property is the current oneof "case", fetching that property will return the value set for that property. Otherwise, fetching the property will return the default value for the property's type - only one member of a oneof can be set at a time.

Setting any constituent property of the oneof will change the reported "case" of the oneof. As with a regular singular field, you cannot set a oneof field with a string or bytes type to a null value. Setting a message-type field to null is equivalent to calling the oneof-specific Clear method.

关于c# - 判断C#中设置了哪个 'oneof' proto3字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com