zoukankan      html  css  js  c++  java
  • Question from one example in Item 5 《Effective C#》

    1. Customer.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace CSharp
    {
        class Customer:IFormattable
        {
            public string Name { get; set; }
            public decimal Revenue { get; set; }
            public string ContactPhone { get; set; }

            string IFormattable.ToString(string format, IFormatProvider formatProvider)
            {
                if (formatProvider != null)
                {
                    ICustomFormatter fmt = formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
                    if (fmt != null)
                    {
                        return fmt.Format(format, this, formatProvider);
                    }
                }

                switch (format)
                {
                    case "r":
                        return Revenue.ToString();
                    case "p":
                        return ContactPhone;
                    case "np":
                        return string.Format("{0,20}, {1,10:c}", Name, Revenue);
                    case "n":
                    case "G":
                    default:
                        return Name;
                }
            }
        }


        public class CustomFormatter : IFormatProvider
        {
            /// <summary>
            ///
            /// </summary>
            /// <param name="formatType"></param>
            /// <returns></returns>
            public object GetFormat(Type formatType)
            {
                if (formatType == typeof(ICustomFormatter))
                    return new CustomerFormatProvider();
                return null;
            }

            //Nested class
            private class CustomerFormatProvider : ICustomFormatter
            {
                public string Format(string format, object arg, IFormatProvider formatProvider)
                {
                    Customer c = arg as Customer;
                    if (c == null)
                    {
                        return arg.ToString();
                    }
                    return string.Format("{0,50}, {1,15}, {2,10:C}", c.Name, c.ContactPhone, c.Revenue);
                }
            }
        }
    }

    2. Program.cs

       class Program
        {
            static void Main(string[] args)
            {

                Customer c1 = new Customer();
                c1.Name = "qiqi";
                c1.Revenue = 13000;
                c1.ContactPhone = "1111111";
                Console.WriteLine(string.Format(new CustomFormatter(), "", c1)); 

                // Expected: call the nested class CustomerFormatProvider::Format in CustomFormatter.

                //    return                                 qiqi,             1111111, ¥13,000.00

                //   But actual result is: empty.

                //   Why?

            }

         }

  • 相关阅读:
    什么是重构,什么不是重构
    Sql Server Merge 关键字 一个条语句实现增删改查
    SQLServer查询一天时间内的数据
    数据库索引原理 二
    重构代码时的7个阶段
    “旁观者效应”是如何毁掉我们的代码
    ClassNotFoundException: org.apache.storm.kafka.spout.KafkaSpoutConfig
    Flume+Kafka整合使用
    SLF4J: Detected both log4joverslf4j.jar AND bound slf4jlog4j12.jar on the class path
    Could not find or load main class org.apache.flume.tools.GetJavaProperty
  • 原文地址:https://www.cnblogs.com/qingxia/p/2018795.html
Copyright © 2011-2022 走看看