现在的位置: 首页 > 综合 > 正文

IFormattable,ICustomFormatter, IFormatProvider接口

2012年05月12日 ⁄ 综合 ⁄ 共 4129字 ⁄ 字号 评论关闭
代码

  class Point : IFormattable
    {
        
public int x, y;
        
public Point(int x, int y)
        {
            
this.x = x;
            
this.y = y;
        }
        
public override String ToString() { return ToString(nullnull); }
        
public String ToString(String format, IFormatProvider fp)
        {
            
if (format == nullreturn String.Format("({0}, {1})", x, y);
            
if (format == "x"return x.ToString();
            
if (format == "y"return y.ToString();
            
throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
        }
    }
    
public sealed class App
    {
        
static void Main()
        {
            Point p 
= new Point(598);
            Console.WriteLine(
"This is my point: " + p.ToString());
            Console.WriteLine(
"The point's x value is {0:x}", p);
            Console.WriteLine(
"The point's y value is {0:y}", p);
            
try
            {
                Console.WriteLine(
"Invalid way to format a point: {0:x+y}", p);
            }
            
catch (FormatException e)
            {
                Console.WriteLine(
"The last line could not be displayed: {0}", e.Message);
            }
            Console.WriteLine(
"yrst {0:C}",11);
            Console.Read();
        }
    }

 

 

 

代码

  public class MyClass : System.IFormattable
    {
        Double d;

        public MyClass(Double d)
        {
            
this.d = d;
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            
return (format == "MyFormat"? "***" + d.ToString(formatProvider) : d.ToString

(format, formatProvider);
        }
    } 

    class Program
    {
        
static void Main(string[] args)
        {
            System.Globalization.CultureInfo culture 
= null;

            MyClass myClass = new MyClass(5);
            
//当IFormatProvider为空时,调用的是当前线程关联的文化信息 
            Console.WriteLine("显示中国货币格式:{0}", myClass.ToString("C"null));

            culture = System.Globalization.CultureInfo.CurrentCulture;
            Console.WriteLine(
"显示当前系统默认货币格式:{0}", myClass.ToString("C", culture));

            culture = new System.Globalization.CultureInfo("zh-HK");
            Console.WriteLine(
"显示香港特别行政区货币格式:{0}", myClass.ToString("C", culture));

            Console.WriteLine("显示我自己定义的货币格式:{0}", myClass.ToString("MyFormat"

null));

            Console.ReadLine(); 
        }
    }

 

 

 

 

 

代码

  public class MyBaseFormat : System.ICustomFormatter, System.IFormatProvider
    {
        
//如果format Type与当前实例类型相同,则为当前实例,否则为空引用 
        public object GetFormat(Type format)
        {
            
if (format == typeof(ICustomFormatter))
                
return this;
            
return null;
        }

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

    class Program
    {
        
static void Main(string[] args)
        {
            
string printString = String.Empty;
            
int i = 100;
            MyBaseFormat myBaseFormat 
= new MyBaseFormat();
            printString 
= string.Format(myBaseFormat, "显示正常格式:{0}", i);
            Console.WriteLine(printString);
            printString 
= string.Format(myBaseFormat, "显示正常格式:{0:C}", i);
            Console.WriteLine(printString);
            printString 
= string.Format(myBaseFormat, "显示自定义格式{0:MyBaseFormat}", i);
            Console.WriteLine(printString);

            Console.ReadLine(); 

        }
    }

 

 

抱歉!评论已关闭.