自定义的转换格式
来源:互联网 发布:shell编程入门 pdf 编辑:程序博客网 时间:2024/05/22 09:51
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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"
自定义的转换格式';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Customizing the Format Method for Custom TypesYou 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"
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 自定义的转换格式
- json格式日期转换为自定义格式
- 将英文格式的日期转换为自定义格式 以及 bigint,int,smallint,tinyint范围。
- dos2unix 批量将dos格式的文件转换成unix格式,支持文件扩展名自定义
- 自定义BeanUtils处理时间格式转换
- js毫秒数自定义时间格式转换
- 自定义标签的格式:
- 日期格式的转换
- 日期格式的转换
- 编码格式的转换
- 时间格式的转换
- 视频文件的格式转换
- 定点格式的转换
- 日期格式的转换
- 时间格式的转换
- 证书格式的转换
- 时间转换的格式
- 日期格式的转换
- C# 2.0 Specification(迭代器)(二)
- css设定表格宽度
- PHP的FTP学习(四)
- 用C#创建Windows(NT)服务
- C#, 程序员的新工具
- 自定义的转换格式
- 吐血奉献:如何搞定DataGrid 分栏的大小(即DataGrid的可视化控制).
- 用文本文件制作留言板提示(上)
- .net里面的数值格式变换
- 杏林同学录(九)
- .NET 将推动开源软件的发展 justleon(原作)
- 杏林同学录(八)
- Picture Numeric Format Strings(我很难解释大家自己看)
- 杏林同学录(七)