[C#]使用IFormattable接口來實現字符串格式化


本文爲原創文章、源代碼爲原創代碼,如轉載/複製,請在網頁/代碼處明顯位置標明原文名稱、做者及網址,謝謝!ide


開發工具:VS2017工具

語言:C#開發工具

DotNet版本:.Net FrameWork 4.0及以上this

1、編寫一個Person類,代碼以下:spa

    class Person
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }
    }

並讓Person類繼承IFormattable,代碼以下:code

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
           //關鍵代碼,後面給出
        }
    }

這裏將會列出須要實現IFormattable的方法ToString(string format, IFormatProvider formatProvider),這裏是關鍵代碼,用來格式字符串,暫時不給出,由後面給出。orm

2、編寫 PersonFormatter類,讓其繼承IFormatProvider及ICustomFormatter,用於對字符串進行格式化,代碼以下:blog

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            //Format實現代碼
        }

        public object GetFormat(Type formatType)
        {
            //GetFormat實現代碼
        }
    }

Format:用於格式化字符串繼承

Format的實現代碼以下:接口

        Person person = arg as Person;
        switch(format)
        {
            case "CH":return $"{person.LastName} {person.FirstName}";
            case "EN":return $"{person.FirstName} {person.LastName}";
            default: return $"{person.LastName} {person.FirstName}";
        }

GetFormat的實現代碼以下:

        if (formatType == typeof(ICustomFormatter)) return this;
        return null;

所以,PersonFormatter類的代碼以下:

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Person person = arg as Person;
            switch(format)
            {
                case "CH":return $"{person.LastName} {person.FirstName}";
                case "EN":return $"{person.FirstName} {person.LastName}";
                default: return $"{person.LastName} {person.FirstName}";
            }
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }
    }

3、實現Person類IFormattable接口ToString方法,代碼以下:

        ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
        if (customFormatter == null) return this.ToString();
        return customFormatter.Format(format, this, null);

最終Person類代碼以下:

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
            if (customFormatter == null) return this.ToString();
            return customFormatter.Format(format, this, null);
        }
    }

4、使用Peson類的ToString方法,編寫如下代碼:

        Person p1 = new Person { FirstName = "XY", LastName = "CN" };
        PersonFormatter pf = new PersonFormatter();
        string s1 = p1.ToString("CN", pf);
        Console.WriteLine(s1);
        string s2 = p1.ToString("EN", pf);
        Console.WriteLine(s2);

5、運行結果:

6、附上完整源碼:

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person { FirstName = "XY", LastName = "CN" };
            PersonFormatter pf = new PersonFormatter();
            string s1 = p1.ToString("CN", pf);
            Console.WriteLine(s1);
            string s2 = p1.ToString("EN", pf);
            Console.WriteLine(s2);
        }
    }

    class PersonFormatter : IFormatProvider,ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Person person = arg as Person;
            switch(format)
            {
                case "CH":return $"{person.LastName} {person.FirstName}";
                case "EN":return $"{person.FirstName} {person.LastName}";
                default: return $"{person.LastName} {person.FirstName}";
            }
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }
    }

    class Person:IFormattable
    {
        public string FirstName { set; get; }
        public string LastName { set; get; }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            ICustomFormatter customFormatter = formatProvider as ICustomFormatter;
            if (customFormatter == null) return this.ToString();
            return customFormatter.Format(format, this, null);
        }
    }
View Code
相關文章
相關標籤/搜索