Where can I find information about the Get, Set, and Address methods for multidimensional instances of System.Array in .NET?

System.Array serves as the base class for all arrays in the Common Language Runtime (CLR) environment. According to this article :

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

and indeed, if I parse this C # code,

 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]); 

in CIL I get

 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...,0...]::Set(int32, int32, 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...,0...]::Set(int32, int32, 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...,0...]::Get(int32, 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...,0...]::Get(int32, int32) IL_004d: call void [mscorlib]System.Console::WriteLine(int32) 

where you can clearly see calls to the above Get and Set methods. It seems that the arity of these methods is related to the dimension of the array, which is apparently due to the fact that they are created by the runtime and are not previously declared. I could not find information about these methods on MSDN, and their simple names make them resistant to Google. I am writing a compiler for a language that supports multidimensional arrays, so I would like to find official documentation about these methods, under what conditions can I expect them to exist, and what can I expect their signatures to be.

In particular, I would like to know if it is possible to get a MethodInfo object for Get or Set for use with Reflection.Emit without having to create an array instance with the correct type and dimension, which is reflected, as is done in the linked example.

+6
c # cil system.array
source share
3 answers

Look here, in particular section 14.2 on pages 63-65

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

But the takeaway, and you can tell from IL, is that they are getter and setter methods for working with arrays at given index positions.

• The Get method, which takes a sequence of int32 arguments, one for each dimension of the array and returns a value whose type is the type of the array element. This method is used to access a specific element of the array where the arguments indicate the index in each dimension, starting with the first, element to be returned.

• The Set method, which accepts a sequence of int32 arguments, one for each dimension of the array, followed by a value whose type is the type of the array element. The return type Set is invalid. This method is used for a specific array element, where the arguments indicate the index in each dimension, starting with the first, the element to be set, and the last argument indicating the value to store in the target element.

• An address method that accepts an int32 argument sequence, one for each array size, and has a return type, which is a managed pointer to the type of array elements. This method is used to return a managed pointer to a specific array element, in which the arguments indicate the index in each dimension, starting with the first one, the element whose address is to be returned.

Edit:. These are pages 63-65, using the pagination of the document. 73-75 in real pdf.

+5
source share

To answer the second question, you do not need to instantiate to get MethodInfo for these methods. Something like

 var mi = typeof(string).MakeArrayType(6).GetMethod("Get"); 

will work to get the Get method for type string[,,,,,] .

+2
source share

I'm not sure if he will consider your specific question, but an excellent text on this (among others) CLR through C # . It is very deep for many topics of interest to you and spends a lot of time with a disassembler that looks at the internal workings of many basic .NET types, including arrays. Definitely worth checking out.

+1
source share

All Articles