当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > IL系列文章之四ArrayinIL续

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 IL系列文章之四ArrayinIL续


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 66 ::
收藏到网摘: n/a

IL系列文章之四:Array in IL (续)上次谈了Array中的一维数组,这次要谈的是多维数组和锯齿形数组。多维数组其实就是数组的数组(好像不如Array of Array那么好听)。上次说过我们用IL可以做一些在C#中不可能做到的事情,比如定义一个数组,它的下界不是从0开始的而是任意一个值。闲话休说,先来看个二维数组的例子。.assembly matrix{}//it is a matrix with 2 dimensions //dim1 from 3 to 5 and dim2 from 4 to 5//the matrix like this could not be defined in C#.class private auto ansi beforefieldinit matrix extends [mscorlib]System.Object{ .method public hidebysig static void Main() cil managed { .entrypoint .maxstack 4 .locals init (int32[,] matrix) ldc.i4.3// load the lower bound for dimension 1 ldc.i4.5// load the upper bound for dim 1 ldc.i4.4// load the lower bound for dim 2 ldc.i4.5// load the upper bound for dim 2 newobj instance void int32[,]::.ctor(int32,int32,int32,int32) // ctor means constructor stloc matrix ldloc matrix ldc.i4.3// index of 1st dimension ldc.i4.4// index of 2nd dimension ldc.i4.s 34// the value call instance void int32[,]::Set(int32,int32,int32)//matrix[3,4] = 34 //a section of error code "matrix[1,1] = 11" //------------------------------------------------------------------ //ldloc matrix //ldc.i4.1 //ldc.i4.1 //ldc.i4.s 11 //call instance void int32[,]::Set(int32,int32,int32) //------------------------------------------------------------------ //it will course an exception "System.IndexOutOfRangeException" ldloc matrix ldc.i4.3 ldc.i4.4 call instance int32& int32[,]::Address(int32,int32) //generate a 32 bit integer instance of matrix element matrix[3,4] //in order to conver it to string for output call instance string [mscorlib]System.Int32::ToString() call void [mscorlib]System.Console::WriteLine(string) ret }}在这个例子中既有二维数组的普通使用方法,又有下界不为0的定义方法。其实我也没有太多好说的了,我要说的大多都写在注释里了。多维数组的定义比一维数组要复杂一些,需要调用专门的构造函数“void int32[,]::.ctor(int32,int32,int32,int32)”来进行构造。还有一点要注意的了,这里使用的是“newobj”,而不是上次所用的“newarr”。就我们一般的二维数组而言下界都是从0开始的,所以实际上我最常用的是这样的一个构造函数“void int32[0…,0…]::.ctor(int32,int32)”,在这个构造函数中,我们只需要提供两个维的上就够了,下界默认从0开始。例子中间有一段注释掉的代码,我利用这段代码来检验IL中定义的非0下界数组是不是真的不从0开始。大家可以试一下这段代码,它一旦运行起来的话系统就会抛出一个"System.IndexOutOfRangeException",看来IL没用骗我们,的确是从我们定义的下界开始的。总的看来虽然二维数组的定义要比一维数组复杂一些,但还是比较规整的。接下来我们要看的就是不太规整的东东了——锯齿数组(Jagged Array)。写文章离不了例子,我还是只有从例子开始。//jagged.csclass jagged{ public static void Main() { int[][] jagged; jagged = new int[3][]; jagged[0] = new int[1]; jagged[1] = new int[2]; jagged[2] = jagged[1]; jagged[1][1] = 11; System.Console.WriteLine(jagged[2][1].ToString()); }}通过它的输出可以看出这个锯齿数组的第3维(jagged[2])和第2维(jagged[1])其实是同一个一维数组,对第2维的处理就是对第3维的处理。有些朋友可能已经看出来了,我的第一个例子——matrix,其实也是锯齿形的,但我们并不称其为锯齿数组。为什么呢?反编译上面的代码,看看真正的锯齿数组是怎么实现的。(和以前一样,为了大家看这方便,我对反编译出来的代码作了一些修改。).assembly jagged{}.class private auto ansi beforefieldinit jagged extends [mscorlib]System.Object{ .method public hidebysig static void Main() cil managed { .entrypoint .maxstack 4 .locals init (int32[][] jagged) ldc.i4.3 newarr int32[]// the jagged array has 3 dimensions //create a array of int pointer stloc jagged// jagged = new int[3][]; ldloc jagged ldc.i4.0//dimension ldc.i4.1//length of dimension newarr [mscorlib]System.Int32//jagged[0] = new int[1]; stelem.ref// set the int[] by reference ldloc jagged ldc.i4.1 ldc.i4.2 newarr [mscorlib]System.Int32 stelem.ref// it is like the first dimension ldloc jagged ldc.i4.2 //{load pionter of 2nd dimension by reference ldloc jagged ldc.i4.1 lde