无法将 RepeatedField 类型的属性或索引器分配给它 — 它是只读的 [英] Property or indexer of type RepeatedField cannot be assigned to — it is read only

查看:62
本文介绍了无法将 RepeatedField 类型的属性或索引器分配给它 — 它是只读的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Google ProtoBuf 文件的新手.我在 ProtoBuf 文件中有以下消息.

I am new to Google ProtoBuf file. I have below message in a ProtoBuf file.

 message AvailabilityOfLockersResp{
  uint32 NumberOfAvailableLockers;
  repeated uint32 lockerIds = 1;
 }

我已经使用 protoc.exe 生成了相应的 ProtoBuf C# 类,并在我的 .NET 项目文件中添加了生成的 C# 类文件.

I have generated the corresponding ProtoBuf C# class using protoc.exe and added that generated C# class file inside my .NET project file.

当我为生成的 LockerIds 字段分配一个值时,我收到错误无法分配属性或索引器 'AvailabilityOfLockersResp.LockerIds' -- 它是只读的":

When I assign a value to the generated LockerIds field I get the error "Property or indexer 'AvailabilityOfLockersResp.LockerIds' cannot be assigned to -- it is read only":

它向我表明它是一个只读字段.但是,我想为此字段分配一个值.如何向该字段添加内容?

It is showing me that it's a read-only field. But, I want to assign a value to this field. How can I add things to this field?

推荐答案

我在上面提出的问题找到了答案.

I found an answer to my own question I asked above.

        List<uint> lockerIds = new List<uint>();

        ProtoPacket protoPacketResponse = new ProtoPacket 
        {               
            AvailabilityOfLockersResp = new AvailabilityOfLockersResp { NumberOfAvailableLockers = (uint)lockerIds.Count() }//LockerIds = lockerIds,
        };

在新实例之外,我已将值分配给 LockerIds,如下所示,

Outside of new instance, I have assigned the value to the LockerIds like below,

protoPacketResponse.AvailabilityOfLockersResp.LockerIds.AddRange(lockerIds);

这篇关于无法将 RepeatedField 类型的属性或索引器分配给它 — 它是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆