频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

自定义的转换格式

来源: 作者: 出处:巧巧读书 2006-09-01 进入讨论组

精华网络内容 :http://www.qqread.com/network/


  Customizing the Format Method for Custom Types

You can easily implement a Format method for your own custom types by overriding the IFormattable Interface. All .NET Framework numeric base types implement IFormattable and have overridden the Format method of that class. In the overridden Format method you can either handle the built-in codes yourself, catch custom codes, or pass codes on to the IFormattable object.

The IFormattable interface combined with the IServiceProvider interface provides the following functionality:

Conversion of numbers, dates and times to strings using a built-in set of specifiers called formatting codes.
Extensibility through overriding of the IServiceProvider object allowing you to extend the format codes offered for base types.
Enhancement of custom types by allowing existing and custom codes to be applied through the Format method.
The following example illustrates the creation of a custom type called MyType. This example adds a custom format specifier of B that will return a binary representation of the type's value.

public class MyType : IFormattable
{
    // a value for our class
    private int MyValue;
    
    // constructor
    public MyType( int value )
    {
        MyValuealue = value;
    }
   //Custom Format method for the type
    public string Format(string format, IServiceObjectProvider sop)
    {
        if (format.Equals ("b"))
         {
            return Convert.ToString (MyValue, 2);
         }
        else {
         return MyValue.Format (format, sop);
         }
    }
}
Customizing the Format method for existing types

By overriding IServiceObjectProvider and ICustomFormatter you can provide additional codes for formatting base types in an already defined class. First, you define a class that implements the two previous mentioned interfaces and overrides GetServiceObject and Format. The following code defines a class that adds a custom Format method that can produce different base-values of an integer:

public class MyFormat : IServiceObjectProvider, ICustomFormatter
{
    //String.Format calls this method to get an instance of a
    //ICustomFormatter to handle the formatting.
    public object GetServiceObject (Type service)
    {
        if (service == typeof (ICustomFormatter))
         {
            return this;
         }
        else{
             return null;
         }
    }
    //Once String.Format gets the ICustomFormatter it calls this format
    //method on each argument.
    public string Format (string format, object arg, IServiceObjectProvider sop)  
    {
        if (format == null)
         {
          return  String.Format ("{0}", arg);
          }
        int i = format.Length;
        //if it's not one of our codes
        if (!format.StartsWith("B"))  
        {
            String temp = "{0:" + format +"}";
             //fall through to the system support
           return String.Format (temp, arg);
        }
        //otherwise, get the base out of the format string and use it to
        //form the output string
        format = format.Trim (new char [] {'B'});
        int b = Convert.ToInt32 (format);
        return Convert.ToString ((int)arg, b);
    }
}
The following code uses the custom Format method defined in MyFormat to display the base-16 representation of MyInt from String.Format:

int MyInt = 42;
String.Format ("{0} in the custom B16 format is {1:B16}", new object [] { MyInt, MyInt }, new MyFormat ());

//returns "42 in custom B16 format is 2a"进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章