首页 > 开发 > 综合 > 正文

自定义的转换格式

2024-07-21 02:16:37
字体:
来源:转载
供稿:网友
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"

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表