/// <summary> /// Builds a person based on user input /// </summary> static Person PromptForAddress(TextReader input, TextWriter output) { Person.Builder person = Person.CreateBuilder(); output.Write("Enter person ID: "); person.Id = int.Parse(input.ReadLine()); output.Write("Enter name: "); person.Name = input.ReadLine(); output.Write("Enter email address (blank for none): "); string email = input.ReadLine(); if (email.Length > 0) { person.Email = email; } while (true) { output.Write("Enter a phone number (or leave blank to finish): "); string number = input.ReadLine(); if (number.Length == 0) { break; } Person.Types.PhoneNumber.Builder phoneNumber = Person.Types.PhoneNumber.CreateBuilder().SetNumber(number); output.Write("Is this a mobile, home, or work phone? "); String type = input.ReadLine(); switch (type) { case "mobile": phoneNumber.Type = Person.Types.PhoneType.MOBILE; break; case "home": phoneNumber.Type = Person.Types.PhoneType.HOME; break; case "work": phoneNumber.Type = Person.Types.PhoneType.WORK; break; default: output.Write("Unknown phone type. Using default."); break; } person.AddPhone(phoneNumber); } return(person.Build()); }
private static void Main() { byte[] bytes; //Create a builder to start building a message Person.Builder newContact = Person.CreateBuilder(); //Set the primitive properties newContact.SetId(1) .SetName("Foo") .SetEmail("foo@bar"); //Now add an item to a list (repeating) field newContact.AddPhone( //Create the child message inline Person.Types.PhoneNumber.CreateBuilder().SetNumber("555-1212").Build() ); //Now build the final message: Person person = newContact.Build(); //The builder is no longer valid (at least not now, scheduled for 2.4): newContact = null; using (MemoryStream stream = new MemoryStream()) { //Save the person to a stream person.WriteTo(stream); bytes = stream.ToArray(); } //Create another builder, merge the byte[], and build the message: Person copy = Person.CreateBuilder().MergeFrom(bytes).Build(); //A more streamlined approach might look like this: bytes = AddressBook.CreateBuilder().AddPerson(copy).Build().ToByteArray(); //And read the address book back again AddressBook restored = AddressBook.CreateBuilder().MergeFrom(bytes).Build(); //The message performs a deep-comparison on equality: if (restored.PersonCount != 1 || !person.Equals(restored.PersonList[0])) { throw new ApplicationException("There is a bad person in here!"); } }