檔案內使用 oneof 如下,類似 C# 的 Switch case 使用
message StudentResponse {
oneof status_code {
Student code = 1;
Error error = 2;
}
}
message Student {
string name = 1;
string class = 2;
map<string, int32> scores = 3;
}
message Error {
int32 code = 1;
string message = 2;
}
C# 端
StudentResponse response = new StudentResponse();
switch (response.StatusCodeCase)
{
case StudentResponse.StatusCodeOneofCase.None:
break;
case StudentResponse.StatusCodeOneofCase.Code:
Student std = response.Code;
break;
case StudentResponse.StatusCodeOneofCase.Error:
Error error = response.Error;
break;
default:
break;
}