自定义格式字符串随笔(IFormattable,IFormatProvider,ICustomFormatter三接口的实现)

相关文章导航
  1. Sql Server2005 Transact-SQL 新兵器学习总结之-总结
  2. Flex,Fms3相关文章索引
  3. FlexAir开源版-全球免费多人视频聊天室,免费网络远程多人视频会议系统((Flex,Fms3联合开发))<视频聊天,会议开发实例8>

 

.NET Framework提供了方法,能够将任何数值、枚举以及日期和时间等基数据类型表示为字符串
格式化由格式说明符字符的字符串控制,该字符串指示如何表示基类型值
例如,格式说明符指示:是否应该用科学记数法来表示格式化的数字
例如:格式字符"C",说明货币格式

同时.NET Framework还使用区域性设置,以便用适合于特定区域性的形式表示基类型。
我们可以提供自定义的区域性设置,或者使用与当前线程关联的默认区域性设置。
例如,格式化货币类型的时候,区域性设置指定用于货币符号

要是我们想拥有自己定义的格式化,.NET Framework也允许我们定义自己格式化方案和自定义区域性设置。
例如:我想格式字符"MyFormat",来说明我自定义的格式,即在字符前加三个***

关于数字格式字符串,可以参考类
System.Globalization.NumberFormatInfo
关于日期与时间格式字符串,可以参考类
System.Globalization.DateTimeFormatInfo

先看看IFormattable接口的原型
public interface IFormattable
{
      // Methods
      string ToString(string format, IFormatProvider formatProvider);
}
参数说明:
format
指定要使用的格式的 String
当为空引用时,表示使用为 IFormattable 实现的类型定义的默认格式
formatProvider
用于格式化该值的 IFormatProvider
当为空引用时,从操作系统的当前区域设置中获取格式信息的

一些基本的值类型实现了该接口,例如:
Int32 ,UInt32 , DateTime ,Guid ,类Enum

再看看IFormatProvider接口的原型
public interface IFormatProvider
{
      // Methods
      object GetFormat(Type formatType);
}
参数说明:
formatType
一个对象,它指定要获取的格式对象的类型

NumberFormatInfo、DateTimeFormatInfo和CultureInfo实现IFormatProvider接口

NumberFormatInfo提供数字格式信息,如用于小数分隔符和千位分隔符的字符,以及货币值中货币符号的拼写和位置
DateTimeFormatInfo提供与日期相关和与时间相关的格式信息,如日期模式中月、日和年的位置
CultureInfo包含特定区域性中的默认格式信息,其中包括数字格式信息以及与日期相关和与时间相关的格式信息

再看看ICustomFormatter接口的原型
public interface ICustomFormatter
{
      // Methods
      string Format(string format, object arg, IFormatProvider formatProvider);
}
参数说明:
format
包含格式规范的格式字符串
arg
要格式化的对象
formatProvider
一个 IFormatProvider 对象,它提供有关当前实例的格式信息


在arg为空引用时,引发异常
如果 format 为空引用 ,将使用默认格式规范
如果 formatProvider 为空引用 ,则忽略该参数

好了,说了这么多
我们来动手来实现格式字符"MyFormat",在字符前加三个***的需求

定义一个类

None.gif using  System;
None.gif
None.gif
namespace  MyFormat
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class MyClass : System.IFormattable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Double d;
InBlock.gif
InBlock.gif        
public MyClass(Double d)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.d=d;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string ToString(string format, IFormatProvider formatProvider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (format=="MyFormat")?"***"+d.ToString(formatProvider):d.ToString(format,formatProvider);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

再到一控制台中

None.gif System.Globalization.CultureInfo culture = null ;
None.gif
None.gif            MyClass myClass
= new  MyClass( 5 );
None.gif            
// 当IFormatProvider为空时,调用的是当前线程关联的文化信息
None.gif
            Console.WriteLine( " 显示中国货币格式:{0} " ,myClass.ToString( " C " , null ));
None.gif
None.gif            culture
= System.Globalization.CultureInfo.CurrentCulture;
None.gif            Console.WriteLine(
" 显示当前系统默认货币格式:{0} " ,myClass.ToString( " C " ,culture));
None.gif
None.gif            culture
= new  System.Globalization.CultureInfo( " zh-HK " );
None.gif            Console.WriteLine(
" 显示香港特别行政区货币格式:{0} " ,myClass.ToString( " C " ,culture));
None.gif
None.gif            Console.WriteLine(
" 显示我自己定义的货币格式:{0} " ,myClass.ToString( " MyFormat " , null ));
None.gif            
None.gif            Console.ReadLine();

效果如下:
format1.gif

如果希望自定义格式化能在多个不同类使用,那么实现我们应该实现ICustomFormatter接口

定义一个类

None.gif using  System;
None.gif
None.gif
namespace  MyFormat
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class MyBaseFormat : System.ICustomFormatter, System.IFormatProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//如果format Type与当前实例类型相同,则为当前实例,否则为空引用
InBlock.gif
        public object GetFormat(Type format)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (format == typeof (ICustomFormatter))
InBlock.gif                
return this;
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//实现Format方法说明:
InBlock.gif        
//如果您的格式方法不支持格式,则确定正在设置格式的对象是否实现 IFormattable 接口。
InBlock.gif        
//如果实现,请调用该接口的IFormattable.ToString 方法。
InBlock.gif        
//否则,调用基础对象的默认 Object.ToString 方法。
InBlock.gif
        public string Format (string format, object arg, IFormatProvider provider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (format == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (arg is IFormattable)
InBlock.gif                    
return ((IFormattable)arg).ToString(format, provider);
InBlock.gif                
return arg.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (format=="MyBaseFormat")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "***"+arg.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (arg is IFormattable)
InBlock.gif                        
return ((IFormattable)arg).ToString(format, provider);
InBlock.gif                    
return arg.ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

到一控制台中

None.gif              string  printString = String.Empty;
None.gif            
int  i = 100 ;
None.gif            MyBaseFormat myBaseFormat
= new  MyBaseFormat();
None.gif
None.gif            printString
= string .Format(myBaseFormat, " 显示正常格式:{0} " ,i);
None.gif            Console.WriteLine(printString);
None.gif            printString
= string .Format(myBaseFormat, " 显示正常格式:{0:C} " ,i);
None.gif            Console.WriteLine(printString);
None.gif            printString
= string .Format(myBaseFormat, " 显示自定义格式{0:MyBaseFormat} " ,i);
None.gif            Console.WriteLine(printString);
None.gif
None.gif            Console.ReadLine();
None.gif

效果如下:
format2.gif

小总结:
1.如果需要您自己的格式化包含在某个类上,在该类上实现IFormattable接口
2.如果希望自定义格式化并使它可供多个不同类使用,那么实现 ICustomFormatter接口



希望上面提到的知识对你有所提示
当然欢迎交流和指正

blog:
http://www.cnblogs.com/aierong
author:aierong
email:aierong@126.com

写随笔费时又费力,请尊重作者的权利
谢谢!

 

 

收藏与分享

收藏到QQ书签 添加到百度搜藏 添加到百度搜藏 add2myweb.gif添加到雅虎收藏 分享到饭否 收藏到就喜欢网络收藏夹 vivi_coop.gif

RSS订阅我  什么是RSS?

feedsky   http://wap.feedsky.com/aierongrss   E-mail
订阅到雅蛙   ico_sub4.gif   使用RSS邮天下订阅   订阅到有道阅读
订阅到抓虾   鲜果阅读器订阅图标   Add to Google
訂閱 Bloglines   哪吒提醒   Subscribe in NewsGator Online

东莞.net俱乐部

东莞.net俱乐部 欢迎您的加入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值