Build() публичный Метод

public Build ( ) : Person,
Результат Person,
Пример #1
0
        /// <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());
        }
Пример #2
0
        public void TestProtocolBuffers()
        {
            // 注意: 这里没办法简单  Person person = new Person();

            Person.Builder builder = new Person.Builder();
            builder.SetEmail("*****@*****.**");
            builder.SetId(123);
            builder.SetName("张三");

            builder.AddPhone(new Person.Types.PhoneNumber.Builder().SetNumber("13012345678").SetType(Person.Types.PhoneType.MOBILE));
            builder.AddPhone(new Person.Types.PhoneNumber.Builder().SetNumber("02112345678").SetType(Person.Types.PhoneType.HOME));

            // 生成 person 类
            Person person = builder.Build();

            // 序列化.
            byte[] buf = person.ToByteArray();

            // 写入文件.
            System.IO.File.WriteAllBytes("test.dat", buf);



            // 读取.
            byte[] dataBuff = System.IO.File.ReadAllBytes("test.dat");


            // 反序列化.
            try
            {
                Person person2 = Person.ParseFrom(dataBuff);
                Console.WriteLine(person2.Id + "," + person2.Name + ", " + person2.Email);
                IList <Person.Types.PhoneNumber> lstPhones = person2.PhoneList;
                foreach (Person.Types.PhoneNumber phoneNumber in lstPhones)
                {
                    Console.WriteLine(phoneNumber.Number + " : " + phoneNumber.Type);
                }
            }
            catch (InvalidProtocolBufferException e)
            {
                // TODO Auto-generated catch block
                Console.WriteLine(e.Message);
            }



            Console.WriteLine("Finish!!!");
            Console.ReadLine();
        }
        public static IEnumerable <object[]> GetAddressBookCases()
        {
            Person.Builder personBuilder = Person.CreateBuilder();
            personBuilder.SetId(1);
            personBuilder.SetName("Foo");
            personBuilder.SetEmail("foo@bar");

            Person.Types.PhoneNumber.Builder phoneBuilder = Person.Types.PhoneNumber.CreateBuilder();
            phoneBuilder.SetType(Person.Types.PhoneType.HOME);
            phoneBuilder.SetNumber("555-1212");
            Person.Types.PhoneNumber phone1 = phoneBuilder.Build();

            personBuilder.AddPhone(phone1);

            Person person1 = personBuilder.Build();

            AddressBook.Builder addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });

            phoneBuilder = Person.Types.PhoneNumber.CreateBuilder();
            phoneBuilder.SetType(Person.Types.PhoneType.MOBILE);
            phoneBuilder.SetNumber("+61 123456789");

            Person.Types.PhoneNumber phone2 = phoneBuilder.Build();
            personBuilder.AddPhone(phone1);
            personBuilder.AddPhone(phone2);

            addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });

            personBuilder = Person.CreateBuilder();
            personBuilder.SetId(2);
            personBuilder.SetName("姓名");
            personBuilder.SetEmail("*****@*****.**");

            personBuilder.AddPhone(phone2);
            personBuilder.AddPhone(phone1);

            Person person2 = personBuilder.Build();

            addressBuilder = AddressBook.CreateBuilder();
            addressBuilder.AddPerson(person1);
            addressBuilder.AddPerson(person2);

            yield return(new object[]
            {
                addressBuilder.Build(),
                false
            });

            yield return(new object[]
            {
                addressBuilder.Build(),
                true
            });
        }