I need to iterate over an array of arbitrary rank. This is for both reading and writing, so GetEnumerator
will not work.
Array.SetValue(object, int)
doesn't work on multidimensional arrays.
Array.SetValue(object, params int[])
would require excessive arithmetic for iterating through the multidimensional space. It would also require dynamic invocation to get around the params
part of the signature.
I'm tempted to pin the array and iterate over it with a pointer, but I can't find any documentation that says that multidimensional arrays are guaranteed to be contiguous. If they have padding at the end of a dimension then that won't work. I'd also prefer to avoid unsafe code.
Is there an easy way to sequentially address a multidimensional array using only a single index?