diff --git a/src/AddressBook/AddressBook.csproj b/src/AddressBook/AddressBook.csproj index 6024b807f7..58bea6a01f 100644 --- a/src/AddressBook/AddressBook.csproj +++ b/src/AddressBook/AddressBook.csproj @@ -1,101 +1,104 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {A31F5FB2-4FF3-432A-B35B-5CD203606311} - Exe - Properties - Google.ProtocolBuffers.Examples.AddressBook - AddressBook - v2.0 - 512 - Google.ProtocolBuffers.Examples.AddressBook.Program - - - - - 3.5 - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - AllRules.ruleset - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - AllRules.ruleset - - - - - - - - - - - - - - - {6908BDCE-D925-43F3-94AC-A531E6DF2591} - ProtocolBuffers - - - - - - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - true - - - False - Windows Installer 3.1 - true - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {A31F5FB2-4FF3-432A-B35B-5CD203606311} + Exe + Properties + Google.ProtocolBuffers.Examples.AddressBook + AddressBook + v2.0 + 512 + Google.ProtocolBuffers.Examples.AddressBook.Program + + + + + 3.5 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + AllRules.ruleset + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + AllRules.ruleset + + + + + + + + + + + + + + + + + + {6908BDCE-D925-43F3-94AC-A531E6DF2591} + ProtocolBuffers + + + + + + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + + + --> \ No newline at end of file diff --git a/src/AddressBook/SampleUsage.cs b/src/AddressBook/SampleUsage.cs new file mode 100644 index 0000000000..ae7b6802d2 --- /dev/null +++ b/src/AddressBook/SampleUsage.cs @@ -0,0 +1,44 @@ +using System; +using System.IO; + +namespace Google.ProtocolBuffers.Examples.AddressBook +{ + class SampleUsage +{ + 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!"); + } +} +}