Working with readonly subcollection in protobuf-net class
I have the following messages in my .proto file:
message Customer
{
optional uint32 user_id = 1;
optional bool logged_in = 2;
repeated AccountPair account_pairs = 3;
}
message AccountPair
{
optional uint32 key = 1;
optional Account value = 2;
}
message Account
{
optional uint32 status_code = 1;
optional string account_number = 2;
optional string account_name = 3;
optional bool store_orders = 4;
}
This creates the following protobuf-net classes (a selection of them, not all).
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Customer")]
public partial class Customer : global::ProtoBuf.IExtensible
{
public Customer() {}
private uint _user_id = default(uint);
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"user_id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(default(uint))]
public uint user_id
{
get { return _user_id; }
set { _user_id = value; }
}
private bool _logged_in = default(bool);
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"logged_in", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(default(bool))]
public bool logged_in
{
get { return _logged_in; }
set { _logged_in = value; }
}
private readonly global::System.Collections.Generic.List<web1.AccountPair> _account_pairs = new global::System.Collections.Generic.List<web1.AccountPair>();
[global::ProtoBuf.ProtoMember(3, Name=@"account_pairs", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<web1.AccountPair> account_pairs
{
get { return _account_pairs; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"AccountPair")]
public partial class AccountPair : global::ProtoBuf.IExtensible
{
public AccountPair() {}
private uint _key = default(uint);
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"key", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(default(uint))]
public uint key
{
get { return _key; }
set { _key = value; }
}
private web1.Account _value = null;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public web1.Account value
{
get { return _value; }
set { _value = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Account")]
public partial class Account : global::ProtoBuf.IExtensible
{
public Account() {}
private uint _status_code = default(uint);
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"status_code", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(default(uint))]
public uint status_code
{
get { return _status_code; }
set { _status_code = value; }
}
private string _account_number = "";
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"account_number", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string account_number
{
get { return _account_number; }
set { _account_number = value; }
}
private string _account_name = "";
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"account_name", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string account_name
{
get { return _account_name; }
set { _account_name = value; }
}
private bool _store_orders = default(bool);
[global::ProtoBuf.ProtoMember(4, IsRequired = false, Name=@"store_orders", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(default(bool))]
public bool store_orders
{
get { return _store_orders; }
set { _store_orders = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
When I need to create an object of the Customer class and serialize it with Protobuf-net, I run into a problem when I try to populate the List of AccountPair in Customer, which is a readonly field named account_pairs. How do I add an account to the Account_pairs list in Customer?
This is what I have tried.
Customer cust = new Customer();
cust.user_id = 101;
cust.logged_in = true;
Account acc = new Account();
acc.status_code = 1;
acc.account_number = "cust1234";
acc.account_name = "jsmith";
acc.store_orders = true;
AccountPair accpair = new AccountPair();
accpair.key = 3;
accpair.value = acc;
cust.account_pairs.Add(accpair);
This last line shows an error adding to read-only field.
Thanks a lot for any help on this.
+3
source to share
No one has answered this question yet
Check out similar questions: