[C#]使用IFormattable接口来实现字符串格式化


本文为原创文章、源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称、作者及网址,谢谢!


开发工具:VS2017

语言:C#

DotNet版本:.Net FrameWork 4.0及以上

一、编写一个Person类,代码如下:

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

并让Person类继承IFormattable,代码如下:

    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),这里是关键代码,用来格式字符串,暂时不给出,由后面给出。

二、编写 PersonFormatter类,让其继承IFormatProvider及ICustomFormatter,用于对字符串进行格式化,代码如下:

    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;
        }
    }

三、实现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);
        }
    }

四、使用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);

五、运行结果:

六、附上完整源码:

    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

 

转载于:https://www.cnblogs.com/cncc/p/8078599.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值