您的位置:首页 > 编程语言 > C#

【CLR Via C#笔记】操作符重载

2008-11-11 16:42 337 查看
System.Decimal 类型是个特殊的类型,他并不直接属于CLR IL的基本类型,而是C#语言自己定义的。(其他值类型比如System.Int32都有对应的IL类型int32)

因此可以通过Reflector查看关于操作符重载和转换操作符的方法

1. 操作符重载

Code

//ILDASM 查看decimal的加号重载

.method public hidebysig specialname static

valuetype System.Decimal op_Addition(valuetype System.Decimal d1,

valuetype System.Decimal d2) cil managed

{

// 代码大小 8 (0x8)

.maxstack 8

IL_0000: ldarg.0

IL_0001: ldarg.1

IL_0002: call valuetype System.Decimal System.Decimal::Add(valuetype System.Decimal,

valuetype System.Decimal)

IL_0007: ret

} // end of method Decimal::op_Addition

//从Int64隐式转化为decimal方法的IL代码

.method public hidebysig specialname static

valuetype System.Decimal op_Implicit(int64 'value') cil managed

{

// 代码大小 7 (0x7)

.maxstack 8

IL_0000: ldarg.0

IL_0001: newobj instance void System.Decimal::.ctor(int64)

IL_0006: ret

} // end of method Decimal::op_Implicit

//从char隐式转化为decimal方法的IL代码

.method public hidebysig specialname static

valuetype System.Decimal op_Implicit(char 'value') cil managed

{

// 代码大小 7 (0x7)

.maxstack 8

IL_0000: ldarg.0

IL_0001: newobj instance void System.Decimal::.ctor(int32)

IL_0006: ret

} // end of method Decimal::op_Implicit

//显式转化为Double型方法的IL代码 注意返回类型是float64即C#中double

.method public hidebysig specialname static

float64 op_Explicit(valuetype System.Decimal 'value') cil managed

{

// 代码大小 7 (0x7)

.maxstack 8

IL_0000: ldarg.0

IL_0001: call float64 System.Decimal::ToDouble(valuetype System.Decimal)

IL_0006: ret

} // end of method Decimal::op_Explicit

//显式转化为Int32.与上面相比函数定义只有返回类型不同

.method public hidebysig specialname static

int32 op_Explicit(valuetype System.Decimal 'value') cil managed

{

// 代码大小 7 (0x7)

.maxstack 8

IL_0000: ldarg.0

IL_0001: call int32 System.Decimal::ToInt32(valuetype System.Decimal)

IL_0006: ret

} // end of method Decimal::op_Explicit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: