aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/AddPerson.cs84
-rw-r--r--examples/CSHARP-README.txt19
-rw-r--r--examples/ListPeople.cs49
3 files changed, 152 insertions, 0 deletions
diff --git a/examples/AddPerson.cs b/examples/AddPerson.cs
new file mode 100644
index 00000000..82d06d6a
--- /dev/null
+++ b/examples/AddPerson.cs
@@ -0,0 +1,84 @@
+// See CSHARP-README.txt for information and build instructions.
+
+using System;
+using System.IO;
+using Tutorial;
+
+class AddPerson {
+ // This function fills in a Person message based on user input.
+ 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:
+ Console.WriteLine("Unknown phone type. Using default.");
+ break;
+ }
+
+ person.AddPhone(phoneNumber);
+ }
+ return person.Build();
+ }
+
+ // Main function: Reads the entire address book from a file,
+ // adds one person based on user input, then writes it back out to the same
+ // file.
+ public static int Main(string[] args) {
+ if (args.Length != 1) {
+ Console.Error.WriteLine("Usage: AddPerson ADDRESS_BOOK_FILE");
+ return -1;
+ }
+
+ AddressBook.Builder addressBook = AddressBook.CreateBuilder();
+
+ if (File.Exists(args[0])) {
+ using (Stream file = File.OpenRead(args[0])) {
+ addressBook.MergeFrom(file);
+ }
+ } else {
+ Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
+ }
+
+ // Add an address.
+ addressBook.AddPerson(PromptForAddress(Console.In, Console.Out));
+
+ // Write the new address book back to disk.
+ using (Stream output = File.OpenWrite(args[0])) {
+ addressBook.Build().WriteTo(output);
+ }
+ return 0;
+ }
+}
diff --git a/examples/CSHARP-README.txt b/examples/CSHARP-README.txt
new file mode 100644
index 00000000..f6dc5aa5
--- /dev/null
+++ b/examples/CSHARP-README.txt
@@ -0,0 +1,19 @@
+To build the C# code:
+
+1) Copy protoc.exe, libprotoc.dll and libprotobuf.dll into
+this directory from the native code output (or use a path when
+running protoc.exe below)
+
+2) Copy Google.ProtocolBuffers.dll from the built C# library code to
+this directory
+
+3) Run this to generate the code:
+protoc --csharp_out=. addressbook.proto
+
+4) Build the AddPerson app:
+csc /r:Google.ProtocolBuffers.dll AddPerson.cs AddressBookProtos.cs
+
+5) Build the ListPeople app:
+csc /r:Google.ProtocolBuffers.dll ListPeople.cs AddressBookProtos.cs
+
+
diff --git a/examples/ListPeople.cs b/examples/ListPeople.cs
new file mode 100644
index 00000000..f4c5f312
--- /dev/null
+++ b/examples/ListPeople.cs
@@ -0,0 +1,49 @@
+// See CSHARP-README.txt for information and build instructions.
+//
+using System;
+using System.IO;
+using Tutorial;
+
+class ListPeople {
+ // Iterates though all people in the AddressBook and prints info about them.
+ static void Print(AddressBook addressBook) {
+ foreach (Person person in addressBook.PersonList) {
+ Console.WriteLine("Person ID: {0}", person.Id);
+ Console.WriteLine(" Name: {0}", person.Name);
+ if (person.HasEmail) {
+ Console.WriteLine(" E-mail address: {0}", person.Email);
+ }
+
+ foreach (Person.Types.PhoneNumber phoneNumber in person.PhoneList) {
+ switch (phoneNumber.Type) {
+ case Person.Types.PhoneType.MOBILE:
+ Console.Write(" Mobile phone #: ");
+ break;
+ case Person.Types.PhoneType.HOME:
+ Console.Write(" Home phone #: ");
+ break;
+ case Person.Types.PhoneType.WORK:
+ Console.Write(" Work phone #: ");
+ break;
+ }
+ Console.WriteLine(phoneNumber.Number);
+ }
+ }
+ }
+
+ // Main function: Reads the entire address book from a file and prints all
+ // the information inside.
+ public static int Main(string[] args) {
+ if (args.Length != 1) {
+ Console.Error.WriteLine("Usage: ListPeople ADDRESS_BOOK_FILE");
+ return -1;
+ }
+
+ // Read the existing address book.
+ using (Stream stream = File.OpenRead(args[0])) {
+ AddressBook addressBook = AddressBook.ParseFrom(stream);
+ Print(addressBook);
+ }
+ return 0;
+ }
+}