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

ASP.NET
使用函数传递参数来执行相应的数据库操作
如何实现在窗体和窗体之间进行传递数据
ASP.NET中文显示之两种解决方法
ASP.NET、JSP及PHP之间的抉择
ASP.NET 2.0发送电子邮件中存在的问题
谈谈HtmlControl与WebControl的区别与用途
从ASP.NET 1.1升级到ASP.NET 2.0要考虑的Cookie问题
通过系统配置来提高ASP.NET应用程序的稳定性
妙用ASP2.0中的URL映射改变网址
AJAX实现web页面中级联菜单的设计
ASP.NET跨页面传值技巧总结
再议ASP.NET DataGrid控件中的“添加新行”功能
Geometry 对象浅析
重构CollapsibleSplitter
如何利用.NET Framework使用RSS feed
ASP.NET获取IP与MAC地址的方法
在ASP.NET 2.0中使用样式、主题和皮肤
ASP.NET中为GridView添加删除提示框
ASP.NET 2.0,无刷新页面新境界
看看一个.net版对话框控件

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 35 ::
收藏到网摘: 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