微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在哪里可以找到有关.NET中多维System.Array实例的Get,Set和Address方法的信息?

System.Array充当公共语言运行时(CLR)中所有数组的基类.根据 this article

For each concrete array type,[the] runtime adds three special methods: Get/Set/Address.

事实上,如果我反汇编这个C#代码,

int[,] x = new int[1024,1024];
x[0,0] = 1;
x[1,1] = 2;
x[2,2] = 3;
Console.WriteLine(x[0,0]);
Console.WriteLine(x[1,1]);
Console.WriteLine(x[2,2]);

进入CIL我得到了,

IL_0000:  ldc.i4     0x400
IL_0005:  ldc.i4     0x400
IL_000a:  newobj     instance void int32[0...,0...]::.ctor(int32,int32)
IL_000f:  stloc.0
IL_0010:  ldloc.0
IL_0011:  ldc.i4.0
IL_0012:  ldc.i4.0
IL_0013:  ldc.i4.1
IL_0014:  call       instance void int32[0...,0...]::Set(int32,int32,int32)
IL_0019:  ldloc.0
IL_001a:  ldc.i4.1
IL_001b:  ldc.i4.1
IL_001c:  ldc.i4.2
IL_001d:  call       instance void int32[0...,int32)
IL_0022:  ldloc.0
IL_0023:  ldc.i4.2
IL_0024:  ldc.i4.2
IL_0025:  ldc.i4.3
IL_0026:  call       instance void int32[0...,int32)
IL_002b:  ldloc.0
IL_002c:  ldc.i4.0
IL_002d:  ldc.i4.0
IL_002e:  call       instance int32 int32[0...,0...]::Get(int32,int32)
IL_0033:  call       void [mscorlib]System.Console::WriteLine(int32)
IL_0038:  ldloc.0
IL_0039:  ldc.i4.1
IL_003a:  ldc.i4.1
IL_003b:  call       instance int32 int32[0...,int32)
IL_0040:  call       void [mscorlib]System.Console::WriteLine(int32)
IL_0045:  ldloc.0
IL_0046:  ldc.i4.2
IL_0047:  ldc.i4.2
IL_0048:  call       instance int32 int32[0...,int32)
IL_004d:  call       void [mscorlib]System.Console::WriteLine(int32)

可以清楚地看到对上述Get和Set方法调用.看起来这些方法的优点与数组的维度有关,这可能是它们由运行时创建并且未预先声明的原因.我无法在MSDN上找到有关这些方法的任何信息,它们的简单名称使它们对谷歌搜索具有抵抗力.我正在为支持多维数组的语言编写一个编译器,所以我想找到一些关于这些方法的官方文档,在什么条件下我可以期望它们存在以及我可以期待它们的签名.

特别是,我想知道是否可以获取Get或Set的MethodInfo对象以与Reflection.Emit一起使用,而不必创建具有正确类型和维度的数组实例,如同在链接的例子.

解决方法

请看这里,特别是第63-65页的第14.2节

http://download.microsoft.com/download/7/3/3/733AD403-90B2-4064-A81E-01035A7FE13C/MS%20Partition%20II.pdf

但是从IL可以看出,它们是在给定索引位置处理数组的getter和setter方法.

• A Get method that takes a sequence of
int32 arguments,one for each
dimension of the array,and returns a
value whose type is the element type
of the array. This method is used to
access a specific element of the array
where the arguments specify the index
into each dimension,beginning with
the first,of the element to be
returned.

• A Set method that takes a sequence
of int32 arguments,followed by a
value whose type is the element type
of the array. The return type of Set
is void. This method is used to set a
specific element of the array where
the arguments specify the index into
each dimension,beginning with the
first,of the element to be set and
the final argument specifies the value
to be stored into the target element.

• An Address method that takes a
sequence of int32 arguments,one for
each dimension of the array,and has a
return type that is a managed pointer
to the array’s element type. This
method is used to return a managed
pointer to a specific element of the
array where the arguments specify the
index into each dimension,beginning
with the first,of the element whose
address is to be returned.

编辑:这是使用文档页面编号的第63-65页.实际PDF中的73-75.

原文地址:https://www.jb51.cc/csharp/243823.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐