国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

自定義的轉換格式

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開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 炎陵县| 长岛县| 正定县| 铁岭市| 桂东县| 平和县| 明溪县| 昌都县| 红桥区| 淮阳县| 德庆县| 巧家县| 罗城| 信宜市| 英山县| 渭源县| 内江市| 巫溪县| 石棉县| 杭锦旗| 竹北市| 朔州市| 宜州市| 静乐县| 宁晋县| 射洪县| 汉沽区| 独山县| 紫阳县| 宽甸| 泾源县| 竹山县| 新建县| 民乐县| 普兰店市| 方山县| 新营市| 新乡县| 榆社县| 图木舒克市| 黄冈市|