Custom conversion formats

Source: Internet
Author: User
Convert customizing the Format method for Custom Types

You can easily implement a Format to your own custom types by overriding the IFormattable. The all. NET Framework Numeric base types implement IFormattable and have overridden the Format by that class. The overridden Format method can be 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 following the 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 T Ypes.
Enhancement of custom types by allowing existing and custom codes to is 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'll 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 can provide additional codes to formatting base types in already defined class. The 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 this can produce different base-values of an integer:

public class Myformat:iserviceobjectprovider, ICustomFormatter
{
String.Format calls this is a 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 S Tring. Format:

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

Returns "The" in custom B16 the format is 2a "



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.