如何生成C#中的多维数组的源代码

Mkr*_*yan 3 .net c# multidimensional-array roslyn coreclr

例如,我们有一个多维的双维数组 double[,] d = new double[1,2];

d.GetType() 回报 {Name = "Double[,]" FullName = "System.Double[,]"}

d[0,0]被编译为call instance float64 float64[0..., 0...]::Get(int32, int32)IL

如何System.Double[,]生成类型的源代码?它是在CLR中烘焙还是Roslyn负责它的产生?

xan*_*tos 6

您正在寻找的是arraynative.cpparraynative.h.

Array.cs开始:

public unsafe Object GetValue(params int[] indices)
Run Code Online (Sandbox Code Playgroud)

使用

fixed(int* pIndices = indices)
    InternalGetReference(&elemref, indices.Length, pIndices);
Run Code Online (Sandbox Code Playgroud)

在哪里InternalGetReference()(同一档案):

[MethodImplAttribute(MethodImplOptions.InternalCall)]
// reference to TypedReference is banned, so have to pass result as pointer
private unsafe extern void InternalGetReference(void * elemRef, int rank, int * pIndices);
Run Code Online (Sandbox Code Playgroud)

MethodImplOptions.InternalCall定义在ecalllist.h(记住这个文件......它包含了所有的MethodImplOptions.InternalCall,所以它是非常有用的)(如果你不记得文件名,你可以简单地搜索InternalGetReference在github上... ...有是不是包含该单词的文件很多):

FCFuncElement("InternalGetReference", ArrayNative::GetReference)
Run Code Online (Sandbox Code Playgroud)

所以你必须寻找ArrayNative,就是我链接的两个文件.