using System; namespace CilExample { //应用程序入口 public class CilApp { static void Main() { Calc calc = new Calc(); int result = calc.Add(10,84); Console.WriteLine("10+84={0}",result);
//按回车结束程序 Console.ReadLine(); } }
//C#计算器 public class Calc { public int Add(int x, int y) { return x + y; } } } 一旦编译器 csc.exe 编译这段代码 就会得到一个单文件*.exe程序集 这个程序集中包含一个程序集清单 CIL指令和描述Calc 与CilApp类的各方面数据 例如如果用ildasm.exe打开该程序集 会发现Add()方法被CIL表示为
.method public hidebysig instance int32 Add(int32 x,int32 y) cil managed { .maxstack 2 .locals int ([0] int32 cs$1$0000) IL_0000:ldarg.1 IL_0001:ldarg.2 IL_0002:add IL_0003:stloc:0 IL_0004:br.s IL_0006 IL_0006:ldarg.0 IL_0007:ret }
Namespace CilExample Module CilApp Sub Main() Dim c As New Calc result = c.Add(10, 84) Console.WriteLine("10+84={0}", result) Console.ReadLine() End Sub padmapper End Module
Class Calc Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer Return x + y End Function End Class
.method public hidebysig instance int32 Add(int32 x,int32 y) cil managed { .maxstack 2 .locals int ([0] int32 Add) IL_0000:nop IL_0001:ldarg.1 IL_0002:ldarg.2 IL_0003:add.ovf IL_0004:stloc.0 IL_0005:br.s IL_0007 IL_0007:ldarg.0 IL_0008:ret }
No comments:
Post a Comment