How is the size of an individual Array element retrieved in C#?
Int16[] a1 = new Int16[1024];
Int16[] a2 = new byte[1024];
Array array = ... // (either a1 or a2)
int elementSizeInBytes = array.____?
sizeof will not work, since sizeof is a compile-time operator -- sizeof(array) or sizeof(array[0]) do not even compile, of course.
It's straightforward to get the number of elements:
int numElements = array.Length;
There doesn't appear to be a method to return the element size. It would then seem to be calculable from the total space allocated for the array / array.Length. But it isn't obvious how to get the total size in bytes used by an Array instance.
Example Usage:
The question is a general one, but the current way I'm trying to use it is to copy the contents of an array buffer to a WriteableBitmap:
private void copyPixels2Bitmap(Array pixels, WriteableBitmap bitmap)
{
int elementSize = pixels.GetValue(0).GetType().___?;
bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight),
pixels, bitmap.PixelWidth * elementSize, 0);
}