日期:2014-05-20  浏览次数:20695 次

IL问题,2段相同的代码结果却不一样
下面是一段IL代码。
0        L_0000:      nop          
1        L_0001:      ldarg.3      
2        L_0002:      ldc.i4.0     
3        L_0003:      newobj       System.Void System.Decimal::.ctor(System.Int32)
4        L_0008:      call         System.Boolean System.Decimal::op_LessThan(System.Decimal,System.Decimal)
5        L_000d:      ldc.i4.0     
6        L_000e:      ceq          
7        L_0010:      stloc.s      V_7
8        L_0012:      ldloc.s      V_7
9        L_0014:      brtrue.s     16 -> ldarg.2
10       L_0016:      ldarg.0      
11       L_0017:      call         System.Web.Mvc.ModelStateDictionary System.Web.Mvc.Controller::get_ModelState()
12       L_001c:      ldstr        "chargeAmount"
13       L_0021:      ldstr        "手续费必须大于等于 0 元"
14       L_0026:      callvirt     System.Void System.Web.Mvc.ModelStateDictionary::AddModelError(System.String,System.String)

翻译成C#就是
 if (chargeAmount < 0m)
   {
       base.get_ModelState().AddModelError("chargeAmount", "手续费必须大于等于 0 元");
   }


再看另一段IL代码
119      L_012d:      nop          
120      L_012e:      ldloc.s      V_5
121      L_0130:      callvirt     System.Decimal Oxite.Model.Account::get_Amount()
122      L_0135:      ldc.i4.0     
123      L_0136:      newobj       System.Void System.Decimal::.ctor(System.Int32)
124      L_013b:      call         System.Boolean System.Decimal::op_LessThanOrEqual(System.Decimal,System.Decimal)
125      L_0140:      ldc.i4.0