Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
0677933d
Commit
0677933d
authored
Aug 14, 2008
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Addressbook samples.
parent
81efcf25
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
0 deletions
+152
-0
AddPerson.cs
examples/AddPerson.cs
+84
-0
CSHARP-README.txt
examples/CSHARP-README.txt
+19
-0
ListPeople.cs
examples/ListPeople.cs
+49
-0
No files found.
examples/AddPerson.cs
0 → 100644
View file @
0677933d
// 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
;
}
}
examples/CSHARP-README.txt
0 → 100644
View file @
0677933d
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
examples/ListPeople.cs
0 → 100644
View file @
0677933d
// 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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment