Ejemplo n.º 1
0
        public static void FormatterTest()
        {
            //SimplePerson person = new SimplePerson() { FirstName = "Jessica", LastName = "Hu", IDCode = "NB123" };
            //Console.WriteLine(person.ToString());
            //PersonFomatter pFormatter = new PersonFomatter();
            //Console.WriteLine(pFormatter.Format("Ch", person, null));
            //Console.WriteLine(pFormatter.Format("Eg", person, null));
            //Console.WriteLine(pFormatter.Format("ChM", person, null));

            SimplePerson person = new SimplePerson()
            {
                FirstName = "Jessica", LastName = "Hu", IDCode = "NB123"
            };

            Console.WriteLine(person.ToString());
            PersonFomatter pFormatter = new PersonFomatter();

            //第一类格式化输出语法
            Console.WriteLine(pFormatter.Format("Ch", person, null));
            Console.WriteLine(pFormatter.Format("Eg", person, null));
            Console.WriteLine(pFormatter.Format("ChM", person, null));
            //第二类格式化输出语法,也更简洁
            Console.WriteLine(person.ToString("Ch", pFormatter));
            Console.WriteLine(person.ToString("Eg", pFormatter));
            Console.WriteLine(person.ToString("ChM", pFormatter));
        }
Ejemplo n.º 2
0
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            SimplePerson person = arg as SimplePerson;

            if (person == null)
            {
                return(string.Empty);
            }

            switch (format)
            {
            case "Ch":
                return(string.Format("{0} {1}", person.LastName, person.FirstName));

            case "Eg":
                return(string.Format("{0} {1}", person.FirstName, person.LastName));

            case "ChM":
                return(string.Format("{0} {1} : {2}", person.LastName, person.FirstName, person.IDCode));

            default:
                return(string.Format("{0} {1}", person.FirstName, person.LastName));
            }
        }