- 热门文章:
- · 如何将powerpoint转换为html
- · css设定表格宽度
- · .Net边学边讲(三)
- · Calling a Button Event from a Compiled DLL
- · datagrid怎么设置分页?
- · .NET之ASP Web Application快速入门(3)(转载)
- · .NET之ASP WebApplication快速入门(4)(转载)
- · .NET之ASP WebApplication快速入门(5)(转载)
- · .NET之ASP Web Application快速入门(1)(转载)
- · .NET之ASP Web Application快速入门(2) (转载)
- · ASP.NET中的错误处理支持
- · ASP.NET中的代码分离
上一篇:日期和时间的转换 >>
自定义的转换格式
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"
下一篇:如何将powerpoint转换为html >>
相关文章:
- · 在ASP.NET中使用AdRotator控件(转)
- · 在ASP.NET中动态生成图形(转)
- · 用ASP.NET加密口令(转)
- · ASP.NET中的错误处理支持(转)
- · ASP.NET中发送Email完整实例(转)
- · ASP.NET中的注释符号
- · 老外编的程序(八):在CSharp里面使用Http Get方法
- · 显示当前浏览器头信息(HEADER)
- · 轮换广告
- · 一个dnslookuo例子。。。
- · beta2的web.config配置
- · 如何在web.config中建立公用的的数据库连接
- · asp.net key considerations(三)
- · asp.net key considerations(二)
- · WebPoll in C#[Vs.net Bate2 等级:中 高]
- · Form code generator V1.1 by Steve Schofield[bate2 等级:中级](转载:aspfree)
- · asp.net key considerations(一)从前用惯了asp的朋友看看这个吧,大家常问的如Request等问题解答得很清楚
- · 使用DataList进行3层编历
- · part1: ShowIssueCat.aspx.cs
- · part2: ShowIssueCat.aspx
- · DataNavigateUrlFormatString的使用方法
- · ASP.NET XML/XSL Transforms(转载www.aspalliance.com)
- · Hiding/Manipulating Databound Items(转载www.aspalliance.com)
- · 无刷新的聊天室的制作兼谈组件制作和ClientSide Script(一)
- · 关于异常捕获
- · 无刷新的聊天室的制作兼谈组件制作和ClientSide Script(二)
- · 转贴:用ASP.NET结合XML制作广告管理程序
- · 转贴:用ASP.NET结合XML制作广告管理程序(2)
- · ASP.NET: Dynamically set Text and Value fields for a DropDownList
- · asp.net优化(二)
- · DotNet中定制自己的表格
- · 回答讨饭猫之asp.net优化(一)
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--8
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--7
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--9
- · 转转: ASP 内建对象Request和Respones
- · 关于.net的自定义控件(请各位大虾指正)
- · 转: ASP.NET高级应用 (2)
